Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用两个外键的作用域_Ruby On Rails_Ruby_Activerecord_Rails Activerecord - Fatal编程技术网

Ruby on rails 使用两个外键的作用域

Ruby on rails 使用两个外键的作用域,ruby-on-rails,ruby,activerecord,rails-activerecord,Ruby On Rails,Ruby,Activerecord,Rails Activerecord,我有以下模式: 我想为两个外键(author\u id和editor\u id)以及单独的外键(例如author\u propositions和editor\u propositions)调用propositions),我需要有延迟或快速加载它们的选项(例如User.includes(:propositions)或不带它的连接) 更新: #I have the scopes which is like this: class User < ActiveRecord::Base has

我有以下模式:

我想为两个外键(
author\u id
editor\u id
)以及单独的外键(例如
author\u propositions
editor\u propositions
)调用
propositions
),我需要有延迟或快速加载它们的选项(例如
User.includes(:propositions)
或不带它的
连接

更新:

#I have the scopes which is like this:
class User < ActiveRecord::Base
  has_many :author_proposals, class_name: 'Proposal', foreign_key: :author_id
  has_many :editor_proposals, class_name: 'Proposal', foreign_key: :editor_id
end

class Proposal < ActiveRecord::Base
  belongs_to :author, class_name: 'User', foreign_key: :author_id
  belongs_to :editor, class_name: 'User', foreign_key: :editor_id
end
#我有如下范围:
类用户

但我需要一个通用的,它会给我所有的建议(包括
作者建议
编辑建议
),它也会加载它们。我是否应该使用
上的条件有许多

如下设置您的关联:

class User < ActiveRecord::Base
  has_many :author_proposals, :class_name => "Proposal", :foreign_key => "author_id"
  has_many :editor_proposals, :class_name => "Proposal", :foreign_key => "editor_id"
end

class Proposal < ActiveRecord::Base
  belongs_to :author, :class_name => 'User', :foreign_key => "author_id"
  belongs_to :editor, :class_name => 'User', :foreign_key => "editor_id"
end
class User < ActiveRecord::Base
  has_many :authored_proposals, class_name: 'Proposal', foreign_key: :author_id
  has_many :editored_proposals, class_name: 'Proposal', foreign_key: :editor_id

  def proposals
    Proposal.where('author_id = :id OR editor_id = :id', { id: id }).distinct
  end
end

class Proposal < ActiveRecord::Base
  belongs_to :author, class_name: 'User', foreign_key: :author_id
  belongs_to :editor, class_name: 'User', foreign_key: :editor_id

  def users
    User.where(id: [author_id, editor_id].uniq)
  end
end
class用户“建议”;:外键=>“作者id”
有很多:编辑建议,:类名称=>“建议”;:外键=>“编辑id”
结束
课程提案'User',:foreign\u key=>“author\u id”
属于\u to:editor,:class\u name=>'User',:foreign\u key=>“editor\u id”
结束

我会这样做:

class User < ActiveRecord::Base
  has_many :author_proposals, :class_name => "Proposal", :foreign_key => "author_id"
  has_many :editor_proposals, :class_name => "Proposal", :foreign_key => "editor_id"
end

class Proposal < ActiveRecord::Base
  belongs_to :author, :class_name => 'User', :foreign_key => "author_id"
  belongs_to :editor, :class_name => 'User', :foreign_key => "editor_id"
end
class User < ActiveRecord::Base
  has_many :authored_proposals, class_name: 'Proposal', foreign_key: :author_id
  has_many :editored_proposals, class_name: 'Proposal', foreign_key: :editor_id

  def proposals
    Proposal.where('author_id = :id OR editor_id = :id', { id: id }).distinct
  end
end

class Proposal < ActiveRecord::Base
  belongs_to :author, class_name: 'User', foreign_key: :author_id
  belongs_to :editor, class_name: 'User', foreign_key: :editor_id

  def users
    User.where(id: [author_id, editor_id].uniq)
  end
end
class用户
您可以执行以下操作:

class User < ActiveRecord::Base
  has_many :authored_proposals, class_name: 'Proposal', foreign_key: :author_id
  has_many :editored_proposals, class_name: 'Proposal', foreign_key: :editor_id

  def proposals
    authored_proposals | editored_proposals
  end
end

class Proposal < ActiveRecord::Base
  belongs_to :author, class_name: 'User', foreign_key: :author_id
  belongs_to :editor, class_name: 'User', foreign_key: :editor_id

  def users
    author | editor
  end
end

发布您为此编写的代码我没有代码,这就是我要问的!:)首选的方法是什么?它是否具有范围并通过lambda?是否使用了
条件
?谢谢。这是针对简单的
作者建议
编辑建议
,但我需要这些建议,它将找到所有建议。这是我最初的想法,我想创建一个新方法,但我需要急切地加载它们。举个例子,这一个不起作用:User.includes(:propositions)。首先,我觉得这不是rails的做法。为什么不干脆
User.includes(:authored\u propositions,:edited\u propositions)
?好主意,我可以添加以下内容:
范围:propositions,->(User){User.includes(:authored\u propositions,:edited\u propositions)}
但当我调用
User.proposements
时,它将始终被加载。如果没有其他rails方法来实现这一点,我会感到惊讶。