Jake Worth

ActiveRecord After Commit Callback

October 05, 20151 min read

Today I used a pretty awesome Rails callback, after_commit.

after_commit executes after a database transaction completes, making it ideal for third-party integrations. Social media and chat applications come to mind.

Here is an example:

after_commit :notify_twitter, on: [:create, :update], unless: -> { draft? || tweeted }

Human version: post to twitter whenever a thing is created or updated (or touched via the touch method), unless it’s a draft or it’s already been tweeted. Don’t post on destroy.

I like the granular control of this callback, and think it’s better in this case than after_save because it covers several more situations. If it fails, that needs to be handled independently from the transaction.


Each week, I write an email about React. It's a collection of quotes, news, conference talks, and documentation curated be me to help you get up to speed on this exploding ecosystem. Join my subscribers today by subscribing to React Explained.

Jake Worth

I'm Jake Worth, a web developer. Home About Now Newsletter