Skip to content

Commit bbc6230

Browse files
authored
Merge pull request rails#49888 from p8/guides/ar-callbacks-ordering
Expand the transaction callback ordering in guides [ci-skip]
2 parents 1040acf + 14acab2 commit bbc6230

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

activerecord/lib/active_record/callbacks.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ module ActiveRecord
241241
#
242242
# config.active_record.run_after_transaction_callbacks_in_order_defined = false
243243
#
244-
# If +true+ (the default from \Rails 7.1), callbacks are executed in the order they
245-
# are defined, just like the example above. If +false+, the order is reversed, so
244+
# When set to +true+ (the default from \Rails 7.1), callbacks are executed in the order they
245+
# are defined, just like the example above. When set to +false+, the order is reversed, so
246246
# +do_something_else+ is executed before +log_children+.
247247
#
248248
# == \Transactions

guides/source/active_record_callbacks.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,9 @@ User was saved to database
610610

611611
### Transactional Callback Ordering
612612

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

615617
```ruby
616618
class User < ActiveRecord::Base
@@ -621,6 +623,15 @@ end
621623

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

626+
This order can be set via configuration:
627+
628+
```ruby
629+
config.active_record.run_after_transaction_callbacks_in_order_defined = false
630+
```
631+
632+
When set to `true` (the default from Rails 7.1), callbacks are executed in the order they
633+
are defined. When set to `false`, the order is reversed, just like in the example above.
634+
624635
[`after_create_commit`]: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_create_commit
625636
[`after_destroy_commit`]: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_destroy_commit
626637
[`after_save_commit`]: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_save_commit

guides/source/configuring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ The default value depends on the `config.load_defaults` target version:
13921392
13931393
#### `config.active_record.run_after_transaction_callbacks_in_order_defined`
13941394
1395-
If true, `after_commit` callbacks are executed in the order they are defined in a model. If false, they are executed in reverse order.
1395+
When `true`, `after_commit` callbacks are executed in the order they are defined in a model. When `false`, they are executed in reverse order.
13961396
13971397
All other callbacks are always executed in the order they are defined in a model (unless you use `prepend: true`).
13981398

0 commit comments

Comments
 (0)