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

Heterogenous FK column / multiple ancestries on same model #555

Closed
kalsan opened this issue Jul 6, 2021 · 2 comments
Closed

Heterogenous FK column / multiple ancestries on same model #555

kalsan opened this issue Jul 6, 2021 · 2 comments

Comments

@kalsan
Copy link

kalsan commented Jul 6, 2021

Hi! This gem looks super interesting but I've got quite a weird use case and I was wondering if there is a way to achieve this with Ancestry.

Imagine two classes, Node and Category:

  • Node has belongs_to: :parent, class_name: 'Node' and belongs_to: :category
  • Category has belongs_to: :parent, class_name: 'Category'

So we can build hierarchies like:

  • A: node112 -> .parent == node11 -> .parent == node1 (which would be ancestry on the parent_id attribute)
  • B: but also node112 -> .category == category12 -> .parent == category1 (where we first go over the the category_id and then parent_id)

Ancestry paths would look something like:

  • A: node1/node11/node112
  • B: category1/category12/node112

So the classes would contain something like:

class Node
  has_ancestry ancestry_column: :parent_id, class: Node # option :class is a fantasy construct here
  has_ancestry ancestry_column: :category_id, class: Category
end

class Category
  has_ancestry ancestry_column: :parent_id, class: Category
end

However for Node this would likely be a problem since ancestry (if I anderstand correctly) would not know that node.parent should go on the parent_id ancestry while node.category would take once the category_id and then the parent_id.

Do I understand correctly that such a structure is currently not possible with ancestry?

Cheers!
Kalsan

@kbrock
Copy link
Collaborator

kbrock commented Aug 12, 2021

closure trees work much better for going across class boundaries.
If you want to use ancestry, STI has worked for many people.

In my main code base, we have created a level of indirection to bridge that gap, but that causes all sorts of nuances that severely impact performance. So I'm in the process of removing those where possible.

Related, or possibly unrelated: people have wanted to have multiple trees per node, but this into in the short term cards.

@kalsan
Copy link
Author

kalsan commented Aug 13, 2021

Thanks, that answers my question.

@kalsan kalsan closed this as completed Aug 13, 2021
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

2 participants