ruby-具有多个foreign_keys的Rails模型has_many

相对于Rails而言,这相对较新,并且尝试使用具有名称,性别,father_id和mother_id(2个父母)的单个Person模型来建模非常简单的家庭“树”。 下面基本上是我想做的,但是显然我不能在has_many中重复:children(第一个被覆盖)。

class Person < ActiveRecord::Base
  belongs_to :father, :class_name => 'Person'
  belongs_to :mother, :class_name => 'Person'
  has_many :children, :class_name => 'Person', :foreign_key => 'mother_id'
  has_many :children, :class_name => 'Person', :foreign_key => 'father_id'
end

有没有一种简单的方法可以将has_many与2个外键一起使用,或者可以根据对象的性别更改外键? 还是有另一种/更好的方式?

谢谢!

Kenzie asked 2020-02-13T17:43:38Z
8个解决方案
43 votes

在IRC上找到了一个可行的简单答案(感谢Radar):

class Person < ActiveRecord::Base
  belongs_to :father, :class_name => 'Person'
  belongs_to :mother, :class_name => 'Person'
  has_many :children_of_father, :class_name => 'Person', :foreign_key => 'father_id'
  has_many :children_of_mother, :class_name => 'Person', :foreign_key => 'mother_id'
  def children
     children_of_mother + children_of_father
  end
end
Kenzie answered 2020-02-13T17:43:54Z
17 votes

为了改善Kenzie的答案,您可以通过定义Person#children来实现ActiveRecord关系,如下所示:

def children
   children_of_mother.merge(children_of_father)
end

查看此答案以获取更多详细信息

stevenspiel answered 2020-02-13T17:44:19Z
9 votes

在Person模型上使用named_scopes做这个:

class Person < ActiveRecord::Base

    def children
      Person.with_parent(id)
    end

    named_scope :with_parent, lambda{ |pid| 

       { :conditions=>["father_id = ? or mother_id=?", pid, pid]}
    }
 end
Zando answered 2020-02-13T17:44:39Z
8 votes

我相信您可以使用:has_one获得所需的关系。

class Person < ActiveRecord::Base
  has_one :father, :class_name => 'Person', :foreign_key => 'father_id'
  has_one :mother, :class_name => 'Person', :foreign_key => 'mother_id'
  has_many :children, :class_name => 'Person'
end

下班后我会确认和编辑这个答案; )

Gordon Wilson answered 2020-02-13T17:45:03Z
4 votes

我更喜欢将范围用于此问题。 像这样:

class Person < ActiveRecord::Base
  belongs_to :father, :class_name => 'Person'
  belongs_to :mother, :class_name => 'Person'
  has_many :children_of_father, :class_name => 'Person', :foreign_key => 'father_id'
  has_many :children_of_mother, :class_name => 'Person', :foreign_key => 'mother_id'

  scope :children_for, lambda {|father_id, mother_id| where('father_id = ? AND mother_id = ?', father_id, mother_id) }
end

这个技巧使不使用实例的孩子变得容易:

Person.children_for father_id, mother_id
squiter answered 2020-02-13T17:45:28Z
3 votes

不能解决上述的一般问题(“有多个外键的has_many”),但是,鉴于一个人可以是母亲或父亲,但不能两者兼而有之,我将添加gender列并与

  has_many :children_of_father, :class_name => 'Person', :foreign_key => 'father_id'
  has_many :children_of_mother, :class_name => 'Person', :foreign_key => 'mother_id'
  def children
    gender == "male" ? children_of_father : children_of_mother
  end
Tom Locke answered 2020-02-13T17:45:51Z
3 votes

我一直在寻找相同的功能,如果您不想返回数组,而是ActiveRecord::AssociationRelation,则可以使用<<而不是+。(请参阅ActiveRecord文档)

class Person < ActiveRecord::Base
  belongs_to :father, :class_name => 'Person'
  belongs_to :mother, :class_name => 'Person'

  has_many :children_of_father, :class_name => 'Person', :foreign_key => 'father_id'
  has_many :children_of_mother, :class_name => 'Person', :foreign_key => 'mother_id'

  def children
     children_of_mother << children_of_father
  end
end
Anthony Mangiavellano answered 2020-02-13T17:46:11Z
3 votes

我对Rails(3.2)中的Associations和(多个)外键的回答:如何在模型中描述它们并编写迁移仅适合您!

至于你的代码,这是我的修改

class Person < ActiveRecord::Base
  belongs_to :father, :class_name => 'Person'
  belongs_to :mother, :class_name => 'Person'
  has_many :children, ->(person) { unscope(where: :person_id).where("father_id = ? OR mother_id = ?", person.id, person.id) }, class_name: 'Person'
end

有什么问题吗?

sunsoft answered 2020-02-13T17:46:40Z
translate from https://stackoverflow.com:/questions/307581/rails-model-has-many-with-multiple-foreign-keys