This callback is called after a record has been created, updated, or destroyed.
You can specify that the callback should only be fired by a certain action with the :on option:
after_commit :do_foo, on: :create after_commit :do_bar, on: :update after_commit :do_baz, on: :destroy after_commit :do_foo_bar, on: [:create, :update] after_commit :do_bar_baz, on: [:update, :destroy]
Note that transactional fixtures do not play well with this feature. Please use the test_after_commit gem to have these hooks fired in tests.
# File activerecord/lib/active_record/transactions.rb, line 225 def after_commit(*args, &block) set_options_for_callbacks!(args) set_callback(:commit, :after, *args, &block) end
The stacking of several after_commit lines is not working. The last line overwrites the ones before.
Using an array for the :on options is also not working.
For details look here: http://github.com/rails/rails/issues/988#issuecomment-12653474
The after_commit and after_rollback callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block. If any exceptions are raised within one of these callbacks, they will be ignored so that they don’t interfere with the other callbacks. As such, if your callback code could raise an exception, you’ll need to rescue it and handle it appropriately within the callback.