Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Allow after_commit :do_something, :on => [:update, :create]

#6660

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 => [...]

Reported by Ary Borenszweig · March 31st, 2011 @ 06:48 PM

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. bgentry
    bgentry
    • 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.

    April 1st, 2011 @ 08:16 PM

  2. bgentry
    bgentry

    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?
    

    April 1st, 2011 @ 08:25 PM

  3. Ary Borenszweig
    Ary Borenszweig

    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)

    April 1st, 2011 @ 08:29 PM

  4. bgentry
    bgentry

    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.

    April 1st, 2011 @ 09:04 PM

  5. Michael Andrews
    Michael Andrews

    I also recently encountered this problem. Here is a unit test demonstrating this behavior:

    https://github.com/rails/rails/pull/269

    April 14th, 2011 @ 02:27 AM

  6. Michael Andrews
    Michael Andrews

    Updated the pull request with a fix to prevent the callbacks in separate contexts from overriding one another.

    April 14th, 2011 @ 03:11 AM

  7. laptopbatteries
    laptopbatteries

    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?

    April 16th, 2011 @ 03:02 AM