51

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 个外键一起使用,或者根据对象的性别更改外键?还是有另一种/更好的方法?

谢谢!

4

8 回答 8

46

在 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
于 2008-11-21T03:22:34.887 回答
17

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

def children
   children_of_mother.merge(children_of_father)
end

有关更多详细信息,请参阅此答案

于 2014-08-16T18:10:48.007 回答
9

在 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
于 2010-03-27T14:13:30.043 回答
6

我相信您可以使用 :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

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

于 2008-11-21T02:44:13.887 回答
4

我更喜欢使用范围来解决这个问题。像这样:

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
于 2013-05-08T20:33:35.127 回答
4

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

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

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

那么有什么问题吗?

于 2016-11-04T16:56:20.910 回答
3

不是对上述一般问题的解决方案(“具有多个外键的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
于 2014-12-12T09:07:02.587 回答
3

我一直在寻找相同的功能,如果您不想返回数组而是 a 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
于 2016-06-07T20:05:10.423 回答