Skip to content

Make AR::Base#touch fire the after_commit and after_rollback callbacks#12031

Closed
airhorns wants to merge 1 commit into
rails:4-0-stablefrom
airhorns:fire_after_commit_on_touch
Closed

Make AR::Base#touch fire the after_commit and after_rollback callbacks#12031
airhorns wants to merge 1 commit into
rails:4-0-stablefrom
airhorns:fire_after_commit_on_touch

Conversation

@airhorns

Copy link
Copy Markdown
Contributor

I expected

class Product < ActiveRecord::Base
  after_commit :expire_cache

  def expire_cache
    puts "cache expired"
  end

product.touch

to output "cache expired", but it doesn't, and it used to in Rails 3.0. This PR brings back that functionality by making touches call after_commit callbacks, woo!

We relied on after_commit callbacks being fired by touches to do things like expire caches, send documents to our search cluster, bump versions in Redis, etc. When we upgraded to 3.2, we found this problem and had to go and add after_touch callbacks to all those places to get touches to cause the same changes in the system. We make pretty heavy use of touches to bump timestamps up the association chain and whatnot, so they are a principle instigator of attribute change in the system. I think having to add both callbacks to really get a callback fired when stuff changes is sucky because it means client code has to deal with de-duplication if both callbacks fire, client code has to be aware of the semantic differences of when they fire, and before this change there is no way to get called back to when a record's timestamp bump has definitely been committed to the database.

Thanks for any feedback you can give me!

@carlosantoniodasilva this is a follow up to #12026 on top of 4-0-stable, is this what you need?

@arthurnn

arthurnn commented Sep 9, 2013

Copy link
Copy Markdown
Member

👍

Comment thread activerecord/test/models/owner.rb Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need one line before on_after_commit method!

@pftg

pftg commented Sep 9, 2013

Copy link
Copy Markdown
Contributor

Please add Changelog

@airhorns

Copy link
Copy Markdown
Contributor Author

@pftg updated, thanks for the feedback

@ryansch

ryansch commented Sep 11, 2013

Copy link
Copy Markdown

👍

@airhorns

Copy link
Copy Markdown
Contributor Author

Bump?

@ryansch

ryansch commented Jan 14, 2014

Copy link
Copy Markdown

We've been running this in production via a monkey patch for 4 months. Works as intended.

@airhorns

Copy link
Copy Markdown
Contributor Author

Us at Shopify too

@carlosantoniodasilva

Copy link
Copy Markdown
Member

Hey folks, sorry I missed this one, already marked it here to take a look as soon as I find some time here. Thanks!

@arthurnn

Copy link
Copy Markdown
Member

I think we wanna close this PR, and open another one against master... =( , than we backport if needed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary?

carlosantoniodasilva added a commit that referenced this pull request Jan 16, 2014
Make AR::Base#touch fire the after_commit and after_rollback callbacks.

Closes #12031.

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/test/cases/integration_test.rb
huoxito added a commit to huoxito/rails that referenced this pull request Mar 14, 2014
As of rails#12031 after_commit and
after_rollback are also executed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants