Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand the transaction callback ordering in guides [ci-skip] #49888

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ module ActiveRecord
#
# config.active_record.run_after_transaction_callbacks_in_order_defined = false
#
# If +true+ (the default from \Rails 7.1), callbacks are executed in the order they
# are defined, just like the example above. If +false+, the order is reversed, so
# When set to +true+ (the default from \Rails 7.1), callbacks are executed in the order they
# are defined, just like the example above. When set to +false+, the order is reversed, so
# +do_something_else+ is executed before +log_children+.
#
# == \Transactions
Expand Down
13 changes: 12 additions & 1 deletion guides/source/active_record_callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,9 @@ User was saved to database

### Transactional Callback Ordering

When defining multiple transactional `after_` callbacks (`after_commit`, `after_rollback`, etc), the order will be reversed from when they are defined.
By default, callbacks will run in the order they are defined. However, when
defining multiple transactional `after_` callbacks (`after_commit`,
`after_rollback`, etc), the order could be reversed from when they are defined.

```ruby
class User < ActiveRecord::Base
Expand All @@ -621,6 +623,15 @@ end

NOTE: This applies to all `after_*_commit` variations too, such as `after_destroy_commit`.

This order can be set via configuration:

```ruby
config.active_record.run_after_transaction_callbacks_in_order_defined = false
```

When set to `true` (the default from Rails 7.1), callbacks are executed in the order they
are defined. When set to `false`, the order is reversed, just like in the example above.

[`after_create_commit`]: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_create_commit
[`after_destroy_commit`]: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_destroy_commit
[`after_save_commit`]: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_save_commit
Expand Down
2 changes: 1 addition & 1 deletion guides/source/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ The default value depends on the `config.load_defaults` target version:

#### `config.active_record.run_after_transaction_callbacks_in_order_defined`

If true, `after_commit` callbacks are executed in the order they are defined in a model. If false, they are executed in reverse order.
When `true`, `after_commit` callbacks are executed in the order they are defined in a model. When `false`, they are executed in reverse order.

All other callbacks are always executed in the order they are defined in a model (unless you use `prepend: true`).

Expand Down