method

after_commit

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.

1Note

Bug - this is not working as documented

beagile ยท Oct 11, 20132 thanks

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