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

Set depth at rebuild! #449

Open
diesl opened this issue May 16, 2021 · 7 comments
Open

Set depth at rebuild! #449

diesl opened this issue May 16, 2021 · 7 comments
Labels

Comments

@diesl
Copy link

diesl commented May 16, 2021

I added Awesome Nested Set to an existing project where we have (scoped) records that are linked with parent_ids. After setting things up, I triggered a rebuild on the model. So far, so good, lft and rgt were set correctly.

However depth was set to 0 for every record. I also could not find a way to trigger a rebuild for depth (only). Do I miss something here?

My brute force workaround was to loop over all records and set depth to level which was resolved to the correct value.

See also similar tickets #314 and #116 that were closed as stale.

@parndt
Copy link
Collaborator

parndt commented May 17, 2021

Thanks @diesl - can you please debug this a little on your local?
If you use bundle open awesome_nested_set to open the gem in your editor, and put debugging statements (like require 'pry';binding.pry or puts) inside the method compute_level then we can maybe see what the code is thinking about your table.

The relevant method is here

def compute_level
node, nesting = determine_depth
node == self ? ancestors.count : node.level + nesting
end

@stale
Copy link

stale bot commented Jun 28, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 28, 2021
@diesl
Copy link
Author

diesl commented Jun 28, 2021

Not stale, I still need to debug

@stale stale bot removed the stale label Jun 28, 2021
@parndt parndt added the pinned label Jun 28, 2021
@mentalic
Copy link

This is still valid. We ended up doing Category.all.each do |c| c.save;end;0 after rebuild in order to recall the depth.

@diesl
Copy link
Author

diesl commented Apr 7, 2022

@parndt Finally I had some time for debugging.

I added a puts statement at the beginning of def compute_level. I did not get any output during Model.rebuild! and after checking the gem code, indeed I do not see any code triggering the computation of the depth during the rebuild:

def rebuild!
# Don't rebuild a valid tree.
return true if model.valid?
root_nodes.each do |root_node|
# setup index for this scope
indices[scope_for_rebuild.call(root_node)] ||= 0
set_left_and_rights(root_node)
reset_counter_cache(root_node)
end
end

I would expect that depth is set during rebuild. Do I have some misconception here?


Seems like you already discovered this behavior many years ago:

Yes, I've been noticing this too.. It seems the depth only gets built when you run save or move_to commands.

Originally posted by @parndt in #116 (comment)

@parndt
Copy link
Collaborator

parndt commented Apr 8, 2022

@diesl thanks - I would expect if depth is being used that it gets set during rebuild! so I think that's how it should work (keen for this to be made to happen!)

@iainbeeston
Copy link

I just noticed this as well. I've also noticed that calling set_depth_for_self_and_descendants! on the root doesn't calculate the depth correctly either. At the moment my workaround is to call rebuild! followed by set_depth! on each node (which will only work for small numbers of nodes...)

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

4 participants