Ruby-on-rails 带有多个外键的 Rails 模型 has_many
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/307581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails Model has_many with multiple foreign_keys
提问by Kenzie
Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, but obviously I can't repeat the :children in a has_many (the first gets overwritten).
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
Is there a simple way to use has_many with 2 foreign keys, or maybe change the foreign key based on the object's gender? Or is there another/better way altogether?
是否有一种简单的方法可以将 has_many 与 2 个外键一起使用,或者根据对象的性别更改外键?或者有另一种/更好的方法吗?
Thanks!
谢谢!
采纳答案by Kenzie
Found a simple answer on IRC that seems to work (thanks to Radar):
在 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
回答by stevenspiel
To improve on Kenzie's answer, you can achieve an ActiveRecord Relation by defining Person#childrenlike:
为了改进Kenzie的答案,您可以通过定义以下内容来实现 ActiveRecord Relation Person#children:
def children
children_of_mother.merge(children_of_father)
end
see this answerfor more details
有关更多详细信息,请参阅此答案
回答by Zando
Used named_scopes over the Person model do this:
在 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
回答by Gordon Wilson
I believe you can achieve the relationships you want using :has_one.
我相信您可以使用 :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
I'll confirm and edit this answer after work ; )
我会在下班后确认并编辑这个答案;)
回答by squiter
I prefer to use scopes for this issue. Like this:
我更喜欢使用范围来解决这个问题。像这样:
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
This trick make it easy to get children without use instances:
这个技巧可以很容易地在没有使用实例的情况下获得孩子:
Person.children_for father_id, mother_id
回答by Tom Locke
Not a solution to the general question as stated ("has_many with multiple foreign keys"), but, given a person can either be a mother or a father, but not both, I would add a gendercolumn and go with
不是如上所述的一般问题的解决方案(“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
回答by Anthony Mangiavellano
I was looking for the same feature, if you don't want to return an array but a ActiveRecord::AssociationRelation, you can use <<instead of +.
(See the ActiveRecord documentation)
我正在寻找相同的功能,如果您不想返回数组而是 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
回答by sunsoft
My answer to Associations and (multiple) foreign keys in rails (3.2) : how to describe them in the model, and write up migrationsis just for you!
我对Rails (3.2) 中的关联和(多个)外键的回答:如何在模型中描述它们,并编写迁移只适合您!
As for your code,here are my modifications
至于你的代码,这是我的修改
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
So any questions?
那么有什么问题吗?

