Model structure for a family tree application

I am building a family tree website for a client where they will be able to add and remove people from the tree that spans many generations. Right now I am using a single Relation model to represent relationships between people. It has an originating_person_id and a target_person_id, which allows me to have add_spouse and add_child method on Person.

I have a lot of duplicated code with two relation types in the one model. I also can only add children to a single person, which then breaks if the person has children with multiple people.

I think I want to have a Person.marriages method that returns spouse and children, as well as a Person.children method that will return children from all marriages.

What is the best way to achieve this? I realise there is a genealogy gem but I am kind of committed to doing this on my own now.

I’ve done a number of apps that have this capability for great-grandparent (no pun intended) to child relationships. The issue is really complicated when you want to be able to search down the tree in any kind of performant way. I’d recommend taking a look at the acts_as_tree gem (GitHub - amerine/acts_as_tree: ActsAsTree -- Extends ActiveRecord to add simple support for organizing items into parent–children relationships.).

Even if you don’t end up using the gem, seeing the approach the authors took (this used to be a Rails-team project) would give you some key foundation knowledge early on.

Hope this helps.