<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/when-should-you-use-after-commit-in-rails--jmpytyrlh" -->

---
title: When Should You Use after_commit in Rails? | daily.dev
description: A deep dive explains why Rails after_save callbacks can enqueue a background job before the surrounding transaction commits, causing intermittent...
canonical: https://daily.dev/posts/when-should-you-use-after-commit-in-rails--jmpytyrlh
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: When Should You Use after_commit in Rails? | daily.dev
og:description: A deep dive explains why Rails after_save callbacks can enqueue a background job before the surrounding transaction commits, causing intermittent...
og:url: https://daily.dev/posts/when-should-you-use-after-commit-in-rails--jmpytyrlh
og:image: https://api.daily.dev/og/posts/JmpytyRLH.png
og:image:alt: When Should You Use after_commit in Rails?
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# When Should You Use after_commit in Rails?

**[RUBYLAND](https://daily.dev/sources/rubyla)** · 14 min read · 1 upvotes · 0 comments

## Summary

A deep dive explains why Rails after_save callbacks can enqueue a background job before the surrounding transaction commits, causing intermittent ActiveRecord::RecordNotFound errors when a worker connects on a different database connection. It traces the mechanics of after_save versus after_commit, shows how Rails 7.2's Account.current_transaction.after_commit and Active Job's enqueue_after_transaction_commit setting (boolean, defaults false in Rails 8.1.3) can fix the ordering, and warns that even after_commit cannot guarantee delivery if the process dies afterward — recommending a durable outbox pattern for critical obligations like billing. It also covers a testing gotcha where transactional test wrappers let after_commit callbacks fire even though the database change is rolled back at teardown.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://railsrevelry.substack.com/p/when-should-you-use-after-commit-in-rails>

## Questions this post answers

### Why does my Sidekiq worker get ActiveRecord::RecordNotFound right after account creation in Rails?

The job likely got enqueued from an after_save callback before the surrounding database transaction committed. after_save runs on the record's own connection while the transaction is still open, so a worker on another connection cannot see the uncommitted row yet under normal isolation rules. Retrying often masks the bug because the next attempt lands after commit.

_Developers chasing transaction-ordering race conditions in Rails compare after_save and after_commit fixes on daily.dev._

### What is the difference between after_save and after_commit in Rails for triggering background jobs?

after_save runs after the SQL write succeeds on the current connection but before the transaction closes, so a raised exception there can still roll back the write; after_commit waits until the entire surrounding transaction commits, guaranteeing other connections can see the row. Use after_save when the extra work belongs in the rollback path, and after_commit when another process must rely on committed state.

_Rails developers weighing callback placement for job enqueuing can track these patterns on daily.dev._

### Does Rails 8.1 defer Active Job enqueuing until after a database transaction commits by default?

No, in Rails 8.1.3 the enqueue_after_transaction_commit setting is a boolean that defaults to false, so jobs are not automatically deferred until commit unless a job class opts in. Rails 7.2 previously used symbols (:default, :always, :never) with :never as the default, and Rails 8.0 changed the job-class default to boolean false while deprecating the symbol values.

_Teams configuring transactional job safety in Rails can follow these version-specific defaults on daily.dev._

## Similar posts on daily.dev

- [Your Transaction Committed. Did the Job Reach the Queue?](https://daily.dev/posts/your-transaction-committed-did-the-job-reach-the-queue--7ios2ch8t) · Ruby Flow · 1 upvotes · 0 comments
- [The Execution Boundary in Rails](https://daily.dev/posts/the-execution-boundary-in-rails-vyn9zhtyw) · RUBYLAND · 0 upvotes · 0 comments
- [Everything you should know about Background Jobs with ActiveJob](https://daily.dev/posts/everything-you-should-know-about-background-jobs-with-activejob-uxyudjznl) · The Miners · 0 upvotes · 0 comments
- [The Rails Way in 2026](https://daily.dev/posts/the-rails-way-in-2026-lwaqok4v6) · arkency · 0 upvotes · 0 comments
- [What Active Record Transactions Actually Protect](https://daily.dev/posts/what-active-record-transactions-actually-protect-zmohboajl) · RUBYLAND · 0 upvotes · 0 comments

---

Tags: [#ruby](https://daily.dev/tags/ruby), [#rails](https://daily.dev/tags/rails)

[View this post on daily.dev](https://daily.dev/posts/when-should-you-use-after-commit-in-rails--jmpytyrlh)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"When Should You Use after_commit in Rails?","url":"https://daily.dev/posts/when-should-you-use-after-commit-in-rails--jmpytyrlh","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/when-should-you-use-after-commit-in-rails--jmpytyrlh"},"datePublished":"2026-08-16T03:58:57.416Z","dateModified":"2026-09-14T07:05:33.495Z","description":"A deep dive explains why Rails after_save callbacks can enqueue a background job before the surrounding transaction commits, causing intermittent...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/85361b3ec5a84d4c59df0adddf28c043?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/85361b3ec5a84d4c59df0adddf28c043?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"RUBYLAND","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"RUBYLAND","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/rubyla","url":"https://daily.dev/sources/rubyla"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/when-should-you-use-after-commit-in-rails--jmpytyrlh","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"ruby,rails","timeRequired":"PT14M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"RUBYLAND","item":"https://daily.dev/sources/rubyla"},{"@type":"ListItem","position":3,"name":"When Should You Use after_commit in Rails?"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/when-should-you-use-after-commit-in-rails--jmpytyrlh#faq","mainEntity":[{"@type":"Question","name":"Why does my Sidekiq worker get ActiveRecord::RecordNotFound right after account creation in Rails?","acceptedAnswer":{"@type":"Answer","text":"The job likely got enqueued from an after_save callback before the surrounding database transaction committed. after_save runs on the record's own connection while the transaction is still open, so a worker on another connection cannot see the uncommitted row yet under normal isolation rules. Retrying often masks the bug because the next attempt lands after commit. Developers chasing transaction-ordering race conditions in Rails compare after_save and after_commit fixes on daily.dev."}},{"@type":"Question","name":"What is the difference between after_save and after_commit in Rails for triggering background jobs?","acceptedAnswer":{"@type":"Answer","text":"after_save runs after the SQL write succeeds on the current connection but before the transaction closes, so a raised exception there can still roll back the write; after_commit waits until the entire surrounding transaction commits, guaranteeing other connections can see the row. Use after_save when the extra work belongs in the rollback path, and after_commit when another process must rely on committed state. Rails developers weighing callback placement for job enqueuing can track these patterns on daily.dev."}},{"@type":"Question","name":"Does Rails 8.1 defer Active Job enqueuing until after a database transaction commits by default?","acceptedAnswer":{"@type":"Answer","text":"No, in Rails 8.1.3 the enqueue_after_transaction_commit setting is a boolean that defaults to false, so jobs are not automatically deferred until commit unless a job class opts in. Rails 7.2 previously used symbols (:default, :always, :never) with :never as the default, and Rails 8.0 changed the job-class default to boolean false while deprecating the symbol values. Teams configuring transactional job safety in Rails can follow these version-specific defaults on daily.dev."}}]}
```

