Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to have children with multiple parents? #367

Open
Sujimichi opened this issue Sep 16, 2020 · 2 comments
Open

how to have children with multiple parents? #367

Sujimichi opened this issue Sep 16, 2020 · 2 comments

Comments

@Sujimichi
Copy link

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?

@ryanb
Copy link

ryanb commented Oct 19, 2020

I had a need for multiple parents too. I saw issue #22 which mentions this is currently not supported by closure tree.

I ended up bypassing closure tree entirely for this. Here's a gist of my solution for multiple parents.

It involves an "edge" join model so it can support multiple parents. Since your constraint is two parents per node there may be a better solution.

@mceachen
Copy link
Collaborator

@ryanb ! Thanks for railscasts! I listened to those ages ago: great work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants