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

no nodes getting a depth of more than 0 #144

Closed
birarda opened this issue Jan 14, 2015 · 1 comment
Closed

no nodes getting a depth of more than 0 #144

birarda opened this issue Jan 14, 2015 · 1 comment
Labels

Comments

@birarda
Copy link

birarda commented Jan 14, 2015

I have a model Place that has a column pointee_id. I use pointee for a polymorphic association, but would like to use closure_tree to get ancestry for the place up to the root. pointee_id typically refers to a place, which allows for the tree structure. Eventually it will point to a different model, and this becomes my root node. I've written a has_many through association to get that root model (since I don't expect closure tree to be able to grab this for me, it's checking the Place model).

The issue I'm having is that after Place.rebuild! or a place update to point to another place, doesn't seem to add the hierarchy properly to the place_hierarchies database. It adds the record for each place that points to itself #<PlaceHierarchy ancestor_id: "7315a306-8c46-449e-91c9-6ce690d050f3", descendant_id: "7315a306-8c46-449e-91c9-6ce690d050f3", generations: 0> but nothing else. All of my PlaceHierarchy records have generations = 0.

Some of the closure tree methods work - I can find the parent and the children properly - but since all records in place_hierarchies have generations = 0, I can't find any ancestors for the node.

My model has the following:

class Place < ActiveRecord::Base
  has_closure_tree parent_column_name: 'pointee_id' 

I ran the closure tree migration for the Place model.

@mceachen
Copy link
Collaborator

closure_tree only supports STI polymorphism. In other words, the parent foreign key column must be a to foreign key to the same table. See http://api.rubyonrails.org/classes/ActiveRecord/Base.html#class-ActiveRecord::Base-label-Single+table+inheritance

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

No branches or pull requests

2 participants