stefankroes / ancestry

Organise ActiveRecord model into a tree structure
MIT License
3.72k stars 458 forks source link

`descendants_count` counter cache? #586

Open ptorrsmith opened 2 years ago

ptorrsmith commented 2 years ago

We're wanting to implement a good way to cache the descendants_count on nodes in a similar way that children_count is set and maintained.

Has anyone attempted extending Ancestry to do this?

Currently we're looking at a pragmatic approach of having an async background job get queued up to update the ancestors of any added/removed node, but thinking this seems like it would be a nice extension for Ancestry.

kbrock commented 2 years ago

The counters are added as a one off and hardcoded into the code itself. They end up needing to be hardcoded into the postgres only extension as well.

It would be great to be able to mixin these changes for those because both types of counters are extensions for the most part.

If you notice, they are defined in the postgres code and has_ancestry code.

sorry, thought I posted this a while ago.

kbrock commented 1 year ago

status update:

Making ok progress at updating the api so we can update the descendants count (hopefully for ruby and sql strategies. Thought I think I'd keep with ruby strategy for now)

kbrock commented 1 year ago

@ptorrsmith curious. Are you still using ancestry? Did you ever end up implementing this? Did you go the background job approach?

ptorrsmith commented 1 year ago

@ptorrsmith curious. Are you still using ancestry? Did you ever end up implementing this? Did you go the background job approach?

Hi. Yes still following this. We ended up baking a post-run call to recalculate it and cache it. Would be good to have this as part of ancestry.