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

Added ability to touch the parent #80

Closed
wants to merge 24 commits into from
Closed

Conversation

seuros
Copy link
Member

@seuros seuros commented Oct 14, 2013

Just add :touch => true to the options of acts_as_tree.

@seuros seuros mentioned this pull request Oct 15, 2013
@mceachen
Copy link
Collaborator

I don't quite understand this request—are you wanting children to force an update on parents? Why?

It looks like this pull request adds the option, but no implementation. If you want to add an implementation with test coverage, I'm happy to take a new PR.

Thanks!

@mceachen mceachen closed this Dec 26, 2013
@seuros
Copy link
Member Author

seuros commented Dec 26, 2013

I'm using closure for nested menus. When a child is updated, the root item need to be touched to invalidate the cache.

@mceachen mceachen reopened this Dec 26, 2013
@mceachen
Copy link
Collaborator

Do you want to make the touch be recursive, or should it only touch the immediate parent?

@seuros
Copy link
Member Author

seuros commented Dec 26, 2013

in my case, i need to invalidated the root.
Active-record has the recursive touching implemented.
But i think we could improve the recursive touching by just 2 queries with closure_tree: (Get the ancestor_ids, touch them at once by updating the updated_at)

@seuros
Copy link
Member Author

seuros commented May 15, 2014

I will send another PR when i write tests

@seuros seuros closed this May 15, 2014
@seuros seuros mentioned this pull request May 16, 2014
@mgwidmann
Copy link

Caching is broken without this feature... sad, have to implement your own touch functionality. 👎

@seuros
Copy link
Member Author

seuros commented Jun 15, 2015

I reimplemented it in another commit. just add touch: true to the ct options

@mgwidmann
Copy link

Well then this feature is broken. Using version 5.2.0 the following code does not work as expected:
(Below I've changed the model and ids, but I did run this code in my app)

child = MyTree.find(123)
parent = Mytree.find(456)
parent.children     # #<ActiveRecord::Associations::CollectionProxy []>
child.parent          # nil
child.update(parent_id: parent.id)
child.updated_at       # Mon, 15 Jun 2015 11:13:38 EDT -04:00
parent.updated_at    # Mon, 15 Jun 2015 10:52:53 EDT -04:00

The two updated_at timestamps should be identical (if not a small fraction of time different). The above shows the parent hasn't been touched in the last 20 minutes, meaning my cache was not busted.

@mgwidmann
Copy link

As the documentation in the README says:

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

It does not touch parents (touching cascades to all children).

@mgwidmann
Copy link

My workaround:

before_save do
    if parent_id_changed?
      self.class.where(id: [parent_id, parent_id_was].compact).update_all(updated_at: Time.now)
    end
  end

Successfully busts the cache when child.update(parent_id: new_parent.id) is called, busting both old parent and new parent. Does not do recursive busting.

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

Successfully merging this pull request may close these issues.

3 participants