This project is archived and is in readonly mode.
Allow after_commit :do_something, :on => [:update, :create]
Reported by Ary Borenszweig | March 31st, 2011 @ 06:48 PM
It's very common to want to do the same thing on an update/create callback after commit (example: create/update a related file, but if the file was there then it is just overwritten).
I tried:
after_commit :do_something, :on => [:update, :create]
but I get:
/Users/asterite/.rvm/gems/ruby-1.9.2-p136@verbo/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:400:in `class_eval': /Users/asterite/.rvm/gems/ruby-1.9.2-p136@verbo/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:414: syntax error, unexpected '[', expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END (SyntaxError)
if (transaction_include_action?(:[:create, :update]))
^
I tried doing this:
after_commit :do_something, :on => :create
after_commit :do_something, :on => :update
but the second callback overrides the first one. So for now I'm using two different methods, or just using an alias_method, but I think the :on => [...] is more DRY, useful and it also similar to the controllers :before_filter, :only => [...]
Comments and changes to this ticket
-
bgentry April 1st, 2011 @ 08:16 PM
- Tag set to callbacks
I just encountered this same bug today and was fortunate to find this bug report from a day ago. I have the same situation and I'd like to run the same actions but only on create/update, not destroy.
I definitely wouldn't expect that using after_commit twice would override the first action.
-
bgentry April 1st, 2011 @ 08:25 PM
Also, I believe you can work around this exact issue by defining a single callback and only running it if the object is persisted:
after_commit :do_something, :if => :persisted?
-
Ary Borenszweig April 1st, 2011 @ 08:29 PM
Yes, thanks! That worked. So what do we do about this? Close as invalid? Still propose to do it? (I will try to make a patch for this)
-
bgentry April 1st, 2011 @ 09:04 PM
I still thing it's a valid bug, the behavior is certainly not as I'd expect. Callbacks are never supposed to override each other like this. At a glance it didn't look like the patch would be very simple, but go ahead and take a crack at it. Hopefully we can get some assistance from the core team.
-
Michael Andrews April 14th, 2011 @ 02:27 AM
I also recently encountered this problem. Here is a unit test demonstrating this behavior:
-
Michael Andrews April 14th, 2011 @ 03:11 AM
Updated the pull request with a fix to prevent the callbacks in separate contexts from overriding one another.
-
laptopbatteries April 16th, 2011 @ 03:02 AM
rolex watches are very common in our Audemars Piguet Replicas lifes, there are quite several well known wrist watches brands, the majority Gucci Replicas of them are Swiss bands, Panerai Replicas ,and it is Omega Replicas unlikely, unless the replica watches 's owner is filthy rich and equally careless replica breitling with his, Even the highest quality, Some replica omega are believed to be to acquire luxury wrist that are Replica Concord founded of gold or platinum or other high priced materials. placing on these wrist fake rolex certainly will make us stand out from other Swiss Replica Watch people.Does everyone can afford these genuine, replica tag heuer Taking your or replica watch ? When should expensive replica watch uk , before you take your precious?
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>