Skip to content

Commit

Permalink
Merge pull request rails#49888 from p8/guides/ar-callbacks-ordering
Browse files Browse the repository at this point in the history
Expand the transaction callback ordering in guides [ci-skip]
  • Loading branch information
p8 authored Nov 2, 2023
2 parents 1040acf + 14acab2 commit bbc6230
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
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

0 comments on commit bbc6230

Please sign in to comment.