Rails模型使用多个外键的has_many关系

51

我对Rails比较新,尝试使用一个Person模型来建立一个非常简单的家庭“树”,其中Person具有姓名、性别、father_id和mother_id(2个父母)。下面基本上是我想要做的事情,但显然我不能在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

有没有一种简单的方法来使用带有2个外键的has_many关联,或者根据对象的性别更改外键?或者还有其他/更好的方法吗?

谢谢!


在Rails 3中,作用域链、ActiveRecord::Relation和最终的has_many:https://dev59.com/0GQm5IYBdhLWcg3wnACR#17476639 - MrYoshiji
你正在寻找“组合键”:https://dev59.com/wmMm5IYBdhLWcg3wFL7s - xpepermint
8个回答

46

在IRC上找到了一个简单的答案,似乎有效(感谢雷达):

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

1
如果存在任何默认作用域,特别是那些可能影响结果顺序的作用域,则此解决方案将无法工作,因为结果不会按预期排序。 - mgadda
您可以通过像我在下面的答案中所做的那样定义#children来获得一个ActiveRecord:Relation - stevenspiel
2
这样不会触发两个SQL查询吗?如果你想添加更多的关系,这可能会变得非常低效。 - stefvhuynh
警告:在Rails 4.2(以及可能是之前的版本),调用children会触发两个SQL请求(一个用于father的子项,另一个用于mother的子项)。对于大型数据集,这不是高效的。@stefvhuynh,感谢您提出这一点。 - philippe_b

17

为了改进Kenzie的回答,你可以通过以下方式定义Person#children来实现ActiveRecord关系:

def children
   children_of_mother.merge(children_of_father)
end

请查看此答案以获取更多详情。


2
警告:正如您提到的答案中所解释的那样,关系是与“AND”合并的。这在此示例中不起作用,因为它意味着您仅选择具有mother_id和(而不是father_id设置为目标ID的人。我不是医生,但这种情况不应该经常发生 :) - philippe_b
.merge 在我需要 AND 的情况下起作用了。谢谢! - Vlad

9

使用Person模型上的命名作用域来实现以下操作:


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

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

工作后我会确认并编辑这个答案;)


这对我没用...看起来太好了,但是在has_many关系中我得到了预期的错误:'people'表中没有名为person_id的列。 - deivid

5

我对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

那么有任何问题吗?


太棒了!在StackOverflow上有几个错误的答案,但这个完美地解决了问题。不过需要快速更正一下:你拼错了mother_id - KurtPreston
@KalleSamuelsson 谢谢你的支持!喜欢你的评论! - sunsoft

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

3
我正在寻找相同的功能,如果您不想返回一个数组而是一个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

铲运算符(<<)实际上会更新数据库记录,而不仅仅是在结果集中引起联合。父亲的孩子现在也将成为母亲的孩子。 - Jacob Vanus

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接