Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:45:48+00:00 2026-06-18T12:45:48+00:00

I need to model up to 5 or 6 generations horse pedigrees using rails/activerecord.

  • 0

I need to model up to 5 or 6 generations horse pedigrees using rails/activerecord. I did my research here on stack and on the web and ultimately utilized this article as the basis of my approach. Here’s what I’ve come up with.

Two models:

Horse has the following attributes id and horse_name
Pedigree has: id, parent_id and horse_id.

And the following associations:

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

This approach is close, however it appears my condition to determine the dam or sire is being applied to the Horse and not the parent. Therefore if the particular horse is Male, the horse.sire will work, but the horse.dam will not and vice versa. Once I get basic functionality working I’d like to add additional methods to get the whole pedigree, grandparents, siblings, descendants, etc.

Questions:

  1. How can I apply the gender condition to the parents and not the horse so that both sire and dam work.

  2. Is the approach that I have take viable or is there a more elegant, efficient way of accomplishing this.

  3. Any other suggestions or guidance would be appreciated.

Apologies for the long question and thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:45:49+00:00Added an answer on June 18, 2026 at 12:45 pm

    I ended up spending a great deal of time on this one, but finally came up with a solution that met my requirements. The associations that ultimately worked follow:

      has_many :parent_horse_relationships, :class_name => "Pedigree", :foreign_key => :horse_id, :dependent => :destroy
    
      has_many :parents, :through => :parent_horse_relationships, :source => :parent do 
        def dam_relationship
          owner = self.proxy_association.owner
          owner = owner.parents.where(:horse_gender => "Female")
          where('pedigrees.parent_id = ?', owner)
        end
    
        def sire_relationship
          owner = self.proxy_association.owner
          owner = owner.parents.where(:horse_gender => "Male")
          where('pedigrees.parent_id = ?', owner)
        end
      end
    
      def dam
        parents.dam_relationship
      end
    
      def sire
        parents.sire_relationship
      end
    

    Question responses:

    I applied the gender condition through use of an association_proxy and a simple wrapper. I created a dam_relationship and corresponding sire_relationship and then wrapped those methods in a couple of dam and sire wrapper methods.

    def dam_relationship
    owner = self.proxy_association.owner
    owner = owner.parents.where(:horse_gender => "Female")
    where('pedigrees.parent_id = ?', owner)
    end
    
    def dam
    parents.dam_relationship
    end
    

    This allows me to do:

    @horse.parents, @horse.dam, @horse.sire (not displayed)
    

    as well as most of the methods included in the ancestry gem mentioned below. With a little bit of recursion it’s fairly straight forward to display the entire pedigree or the number of generations that interest you.

    I decided that the approach of having two models (Horse and Pedigree) provide som additional flexibility compared to having the sire_id and dam_id directly in the Horse model. This approach will enable me to more easily create methods like @horse.uncle, @horse.aunt. I believe these would be more difficult with the sire_id and dam_id directly in the Horse model.

    The most popular gem for accomplishing this seems to be ancestry. The author accomplishes this and a lot more simply by adding an ancestry column to the model of interest. Its a very nice solution a definitely worth checking out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to model-bind data from textboxes generated dynamically on the client side (using
I'm working on a Web service in Django, and I need to model a
In ActiveRecord model after_save callback I need to ROLLBACK transaction and return false. def
Both my Rails model and controller code need to write files to the file
should a non-database data class I need be created as a Rails 3 model,
I have a need for a model(?) on my app which basically contains a
I need to load a Model in a component to save the Data of
I need to load a model, existing of +/- 20 tables from the database
I need to validate a byte[] in my model as Required but whenever I
I need to figure out a way to set ValidateRequest at model...I understand that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.