ActiveRecord after_commit hook

Published on Author Akhil Bansal1 Comment

Most developers use ActiveRecord callbacks after_create/after_update/after_destroy to generate background job, expire cache, etc., but they don’t realize these callbacks are still wrapped in database transaction, they probably got unexpected results like the app queued a job but the record is not created/updated(transaction rolled back).
For such situations after_commit callback should be used. after_commit is introduced from rails 3, if you use rails 2, there is a gem called after_commit( after_rollback is another callback introduced in rails 3.

Here is the article with example.

One Response to ActiveRecord after_commit hook

  1. Hi, this helped a lot – I didn’t know there was an after_commit callback.

    I had a situation where committing to the database took some time, and in the meantime my background job tried to access that record (and it was not saved jet). It was confusing but this post saved the day 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *