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

Ancestry with an "intermediate" model (and table) #520

Open
marino-mrc opened this issue Dec 4, 2020 · 1 comment
Open

Ancestry with an "intermediate" model (and table) #520

marino-mrc opened this issue Dec 4, 2020 · 1 comment

Comments

@marino-mrc
Copy link

Hi, it is possible to use ancestry with an "intermediate" model? I mean, I cannot modify the table where I need to use ancestry (for example User) and I'd like to use another model (like AncestryUser) in order to manage user's ancestry. It is possible? If yes, can you submit an example? Thank you

@kbrock
Copy link
Collaborator

kbrock commented Dec 8, 2020

I would not advise that.
We have an intermediate table, but with a polymorphic reference to the records so your hierarchy can be with multiple records. Having said that, note that I am actively removing where possible and am not the biggest fan.

I've run into race conditions that ends up creating multiple intermediate records for a single primary record.
Hardcoding the id to be the same as the primary table may get around this.
Also, then the ids in the intermediate's ancestry hierarchy value would match the ids in the primary table.

I also found the bouncing through the intermediate table to take a toll on the queries.

Not sure how good your rails and SQL is, but you may be able to use sub-selects to bring back the ancestry value in a select clause and have much of what you want working. But it has the potential to be confusing and quirky.

You may want to take a look at the closure_tree gem. It's data model works closer to what you have described.

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