相對較新的軌道,並試圖用一個名稱,性別,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