awesome_nested_set icon indicating copy to clipboard operation
awesome_nested_set copied to clipboard

Set depth at rebuild!

Open diesl opened this issue 3 years ago • 7 comments

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.

diesl avatar May 16 '21 11:05 diesl

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 https://github.com/collectiveidea/awesome_nested_set/blob/36ec2b072c797be66dd588fbd92780573e3c57c0/lib/awesome_nested_set/model/relatable.rb#L98-L102

parndt avatar May 17 '21 22:05 parndt

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[bot] avatar Jun 28 '21 22:06 stale[bot]

Not stale, I still need to debug

diesl avatar Jun 28 '21 22:06 diesl

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.

mentalic avatar Aug 18 '21 04:08 mentalic

@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:

https://github.com/collectiveidea/awesome_nested_set/blob/36ec2b072c797be66dd588fbd92780573e3c57c0/lib/awesome_nested_set/tree.rb#L18-L28

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 https://github.com/collectiveidea/awesome_nested_set/issues/116#issuecomment-3694189

diesl avatar Apr 07 '22 21:04 diesl

@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!)

parndt avatar Apr 08 '22 00:04 parndt

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...)

iainbeeston avatar Sep 22 '22 11:09 iainbeeston