Skip to content

how to have children with multiple parents? #367

Open
@Sujimichi

Description

@Sujimichi

How would one go about setting up a the association to model a human (or any organic) family tree, where each node has two parents? Is this possible with closure_tree?

I have a Human class and in my initial setup (with a simple self join) I had:

class Human < ApplicationRecord
  belongs_to :father, class_name: "Human", optional: true
  belongs_to :mother, class_name: "Human", optional: true

  has_many :fathered_offspring, class_name: "Human", foreign_key: :father_id
  has_many :mothered_offspring, class_name: "Human", foreign_key: :mother_id
    
    
  def offspring
    self.mothered_offspring.or(self.fathered_offspring)
  end
end

This works, in that a human can have human.mother and human.father and both mother and father can call .offspring
But obv this setup will become horribly inefficient when trying to draw large family trees, so I'm looking to see if closure_tree could help.

If I add to my existing Human model

has_closure_tree parent_column_name: :father_id

Then closure_tree works for the paternal line. But how would I set about adding a maternal line?
I optimistically tried adding has_closure_tree parent_column_name: :mother_id after the first has_closure_tree but that (not surprisingly) didn't work.

Is having multiple parents possible with closure_tree? If not, is there a standard, conventional way to approach this sort of problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions