ClosureTree / closure_tree

Easily and efficiently make your ActiveRecord models support hierarchies
https://closuretree.github.io/closure_tree/
MIT License
1.84k stars 239 forks source link

Passing touch along to ancestors instead of descendants (for cache expiry of nested content)? #253

Open nruth opened 7 years ago

nruth commented 7 years ago

:touch delegates to the belongs_to annotation for the parent, so touching cascades to all children (the performance of this for deep trees isn't currently optimal).

So, when a parent is updated it passes on the timestamp change to its children (descendants?).

I'd like the reverse behaviour, with an updated child passing its timestamp changes up to all of its ancestors. Something like this:

after_save :touch_ancestors_for_cache_expiry

def touch_ancestors_for_cache_expiry
  ancestors.to_a.each(&:touch)
end

Would it make sense to support both of these in the has_closure_tree call?

mceachen commented 7 years ago

I haven't needed this, but if you want to build it (please include tests!), I'm welcome to a PR!

SirRawlins commented 7 years ago

This is certainly something which I'd like to see myself. @nruth did you ever make any progress on this? My guess is that one concern might be around creating an infinite loop?

i.e. A descendant touches its parent, which touches its descendant, which touch parent etc etc...

In my given use case, I'd ideally like an object to touch all associated descendants and ancestors - how viable that is I'm not sure.