rails / activerecordを使用して、最大5世代または6世代の馬の血統をモデル化する必要があります。私はここでスタックとWebで調査を行い、最終的にこの記事をアプローチの基礎として利用しました。これが私が思いついたものです。
2つのモデル:
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は機能しません。その逆も同様です。基本的な機能が機能するようになったら、血統全体、祖父母、兄弟、子孫などを取得するためのメソッドを追加したいと思います。
質問:
父と母の両方が機能するように、どうすれば馬ではなく親に性別条件を適用できますか。
私が採用したアプローチは実行可能ですか、それともこれを達成するためのよりエレガントで効率的な方法がありますか。
その他の提案やガイダンスをいただければ幸いです。
長い質問をお詫びし、あなたの助けに感謝します。