相对较新的轨道,并试图用一个名称,性别,father_id和mother_id(父母)的单人模型来建模一个非常简单的家庭“树”。下面基本上是我想要做的,但显然我不能重复:has_many中的孩子(第一个被覆盖)。Rails模型has_many与多个foreign_keys
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中,范围chainning,ActiveRecord的::关系,并最终`has_many`:http://stackoverflow.com/questions/17476521/rails-has-many-custom-activerecord-association/17476639#17476639 – MrYoshiji 2013-07-04 19:02:21
你是寻找“组合键”:http:// stackoverflow。com/questions/17882105/is-it-it-possible-to-define-composite-primary-key-for-table-using-active-record – xpepermint 2014-02-27 12:53:13