【发布时间】:2013-02-09 23:17:49
【问题描述】:
我需要使用 rails/activerecord 为最多 5 或 6 代马谱系建模。我在这里对堆栈和网络进行了研究,并最终利用article 作为我方法的基础。这是我想出的。
两种型号:
Horse has the following attributes id and horse_name
Pedigree has: id, parent_id and horse_id.
还有以下关联:
has_many :parent_horse_relationships, :class_name => "Pedigree", :foreign_key => :horse_id, :dependent => :destroy
has_one :sire_horse_relationship, :class_name => "Pedigree", :foreign_key => :horse_id, :conditions => "horse_gender = 'Male'
has_one :dam_horse_relationship, :class_name => "Pedigree", :foreign_key => :horse_id, :conditions => "horse_gender = 'Female'
has_many :parents, :through => :parent_horse_relationships, :source => :parent
has_one :sire, :through => :sire_horse_relationship,:source => :parent
has_one :dam, :through => :dam_horse_relationship,:source => :parent
has_many :horse_parent_relationships, :class_name => "Pedigree", :foreign_key => :parent_id, :dependent => :destroy
has_many :progenies, :through => :horse_parent_relationships, :source => :horse
这种方法很接近,但我的条件似乎是确定母系或父系适用于马而不是父系。因此,如果特定的马是雄性,则 horse.sire 会工作,但 horse.dam 不会,反之亦然。一旦基本功能正常工作,我想添加其他方法来获取整个谱系、祖父母、兄弟姐妹、后代等。
问题:
如何将性别条件应用于父母而不是马,以便父亲和母亲都能发挥作用。
我采用的方法是否可行,或者是否有更优雅、更有效的方法来实现这一目标。
如有任何其他建议或指导,我们将不胜感激。
抱歉,问题很长,感谢您的帮助。
【问题讨论】:
标签: ruby-on-rails-3 activerecord associations