skip to content

In a Rails 8.1 model declaring after_create_commit :sync_index and after_update_commit :sync_index, why does a newly created gym member never get indexed?

level: seniorimportance: should knowfreq 38%

answer

  1. commit callbacks deduplicated by filter
  2. the last declaration wins
  3. after_save_commit covers both
  4. declared order since load_defaults 7.1
  5. create then update: create commit only

basics

~10 s

Both macros register after_commit :sync_index, and Rails keeps one callback per filter, so the update-only declaration replaces the create one. Use after_save_commit :sync_index, or after_commit :sync_index, on: [:create, :update].

solid answer

~40 s

`after_create_commit` and `after_update_commit` are shortcuts: each calls `set_callback(:commit, ...)` with an `on:` condition. Active Support removes an existing callback with the same filter before adding a new one, so the second declaration, the update-only one, silently replaces the first. New members commit, nothing fires, and only later edits trigger `sync_index`. The fix is one declaration covering both actions: `after_save_commit :sync_index` or `after_commit :sync_index, on: [:create, :update]`. Two related Rails 8.1 facts usually come up next. With `load_defaults` 7.1 or later, several commit callbacks run in the order declared; older defaults ran them in reverse. And a record created and then updated in one transaction gets a single commit callback, `after_create_commit`, run once after the final `COMMIT`.

code

ruby · 11 lines
ruby
# Broken: the second macro replaces the first
class GymMember < ApplicationRecord
  after_create_commit :sync_index
  after_update_commit :sync_index
end

# Fixed: one callback that covers both actions
class GymMember < ApplicationRecord
  after_save_commit :sync_index
  # or: after_commit :sync_index, on: [:create, :update]
end

go deeper

for a junior

Remember that after_save_commit covers create and update, and that declaring the same method in two commit macros keeps only the last one.

for a middle

Explain that the commit shortcuts all register after_commit with an on: condition and that Active Support deduplicates callbacks by filter, ignoring the condition.

for a senior

Diagnose silent gaps in external systems from callback declarations, and know how load_defaults changes commit-callback order and which instance gets the callbacks.

for a principal

Treat framework-default flags as part of the upgrade plan; behaviour such as callback order changes only when load_defaults moves, so audit it before trusting old assumptions.

## Why the second declaration wins The commit macros are thin wrappers. `after_create_commit :sync_index` is `after_commit :sync_index, on: :create`, and `after_update_commit :sync_index` is `after_commit :sync_index, on: :update`. Both end in `set_callback(:commit, :after, :sync_index, ...)`. When a callback is added, Active Support first deletes any callback in that chain that **duplicates** it, and a symbol filter duplicates another callback with the same symbol and kind. The `on:` condition is not part of that comparison. So: 1. `after_create_commit :sync_index` adds a commit callback guarded by "this transaction created the record". 2. `after_update_commit :sync_index` removes it and adds one guarded by "this transaction updated the record". 3. A new member commits, the remaining guard is false, and nothing is indexed. The Rails documentation states it plainly: defining the same method with `after_commit`, `after_create_commit` and `after_save_commit` leaves only the last one. ## The fixes | Declaration | Fires after commit of | Deduplicated against other `:sync_index` commit callbacks? | |---|---|---| | `after_create_commit :sync_index` + `after_update_commit :sync_index` | update only | yes, the last wins | | `after_save_commit :sync_index` | create and update | one callback, nothing to clash with | | `after_commit :sync_index, on: [:create, :update]` | create and update | one callback, nothing to clash with | | `after_commit :sync_index` | create, update and destroy | one callback, nothing to clash with | Different method names never clash, so `after_create_commit :send_welcome_sms` next to `after_save_commit :sync_index` is fine. ## Order of several commit callbacks - With `config.load_defaults 7.1` or later, which includes a new 8.1 app, `run_after_transaction_callbacks_in_order_defined` is `true`, so `after_commit` and `after_rollback` callbacks run **in the order declared**, like every other callback. - The framework's own default is `false`, which runs them in **reverse** order. Apps upgraded without moving their defaults still behave that way. - `prepend: true` moves a commit callback ahead of those already declared; a bug in how it combined with the in-order setting was fixed only in Rails 8.1.4, so check the patch level before relying on it. ## One record, many saves in one transaction - Commit callbacks run **once per record** per transaction, after the outermost `COMMIT`, however many times it was saved. - A record **created and then updated** in the same transaction counts as a create: `after_create_commit` runs and `after_update_commit` does not, because the transaction is judged by what happened to the row overall. - When two different Ruby objects load and save the same row inside one transaction, only one of them receives the callbacks. With 7.1 defaults (`run_commit_callbacks_on_first_saved_instances_in_transaction = false`) Rails picks the instance with the freshest state, normally the last one to save; older defaults picked the first. ## Why Rails behaves this way Deduplication is also why redeclaring a named callback, in a subclass or a reopened class, does not make it run twice, which is usually what you want for something like `before_save :assign_member_number`. The commit shortcuts reuse the same chain with an `on:` condition attached, so they inherit that rule even though the conditions differ. The Rails API docs for `after_commit` call this out and recommend the `on:` option with an array for exactly this case. ## Confirming it in a console The chain is inspectable. `GymMember._commit_callbacks.map(&:filter)` returns the registered filters; with the broken declarations it lists `:sync_index` once, not twice. That single entry, combined with an `on: :update` guard, is the whole bug. Blocks are not deduplicated, because only symbol filters are compared, so two `after_commit do ... end` blocks both run; the trap is specific to named methods, which is exactly what most codebases use. ## Production checklist - Grep for the same method name across `after_*_commit` macros; a linter rarely catches it. - Test both paths: create a member and assert the index job was enqueued, then update one and assert again. - Keep commit callbacks idempotent, because a create-then-update sequence collapses into one call. - Check `config.load_defaults` before trusting callback order in an upgraded app. The bug is typical of callbacks in general: nothing errors, the code reads correctly, and the only symptom is missing data in another system.

  • In a new Rails 8.1 app, in what order do two after_commit callbacks on the same model run?
    In the order they are declared. A new app runs `load_defaults 8.1`, which includes the 7.1 setting `run_after_transaction_callbacks_in_order_defined = true`. Without it, as in an upgraded app still on older defaults, transactional `after_` callbacks run in reverse order.
  • A member is created and then updated inside one transaction. Which commit callbacks fire?
    Only the create ones, once. Rails runs commit callbacks per record after the final `COMMIT` and treats a row that did not exist when the transaction began as created, so `after_create_commit` and `after_save_commit` run while `after_update_commit` does not.

  • after_create_commit and after_update_commit are separate chains, so both fire
  • Commit callbacks run in reverse declaration order in a new Rails 8.1 app
  • Saving the same record twice in one transaction fires after_commit twice
  • after_save_commit also runs when the record is destroyed
  • The on: option makes two callbacks with the same method distinct

Assumes Rails 8.1 with load_defaults 7.1 or later; on older defaults after_commit callbacks run in reverse order and on the first saved instance.