after_commit(*args, &block) public

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

Also, to have the callback fired on create and update, but not on destroy:

after_commit :do_zoo, :if => :persisted?

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.

Show source
Register or log in to add new notes.
October 11, 2013 - (<= v3.2.13)
2 thanks

Bug - this is not working as documented

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

March 5, 2013 - (v3.0.0 - v3.2.8)
0 thanks

Exceptions raised within are ignored.

From http://guides.rubyonrails.org/active_record_validations_callbacks.html

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.