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

The ordering can now be configured.

Also update the Configuring guide and API docs. In the Configuring guide
"When true" seems more common than "When false", so that is used
instead.

Co-authored-by: Akhil G Krishnan <akhilgkrishnan4u@gmail.com>
This commit is contained in:
Petrik 2023-11-01 21:10:31 +01:00
parent 8e73e620b1
commit 14acab20a2
3 changed files with 15 additions and 4 deletions

@ -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

@ -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
@ -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

@ -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`).