我对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关联,或者根据对象的性别更改外键?或者还有其他/更好的方法吗?
谢谢!
has_many
:https://dev59.com/0GQm5IYBdhLWcg3wnACR#17476639 - MrYoshiji