aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/callbacks.rb
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-01-06 04:39:31 -0500
committerGitHub <noreply@github.com>2017-01-06 04:39:31 -0500
commitb3bd6451b13307cfd2349b74191b10f99a607f66 (patch)
treedf3a9a911eaaddcfa1e555da0731763a8a622b99 /activerecord/lib/active_record/callbacks.rb
parent5eff7a9ca7bb2ee7f16db1ab4d11cebe28757ba5 (diff)
parent8f90e87b90e77bcde2a5e2e2f3764ed86a8f9639 (diff)
downloadrails-b3bd6451b13307cfd2349b74191b10f99a607f66.tar.gz
rails-b3bd6451b13307cfd2349b74191b10f99a607f66.tar.bz2
rails-b3bd6451b13307cfd2349b74191b10f99a607f66.zip
Merge pull request #27294 from eavgerinos/doc-ar-callbacks-order
[documentation] ActiveRecord: Document order of Callbacks
Diffstat (limited to 'activerecord/lib/active_record/callbacks.rb')
-rw-r--r--activerecord/lib/active_record/callbacks.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb
index be6720ddf3..eb44887e18 100644
--- a/activerecord/lib/active_record/callbacks.rb
+++ b/activerecord/lib/active_record/callbacks.rb
@@ -225,6 +225,55 @@ module ActiveRecord
#
# This way, the +before_destroy+ gets executed before the <tt>dependent: :destroy</tt> is called, and the data is still available.
#
+ # Also, there are cases when you want several callbacks of the same type to
+ # be executed in order.
+ #
+ # For example:
+ #
+ # class Topic
+ # has_many :children
+ #
+ # after_save :log_children
+ # after_save :do_something_else
+ #
+ # private
+ #
+ # def log_chidren
+ # # Child processing
+ # end
+ #
+ # def do_something_else
+ # # Something else
+ # end
+ # end
+ #
+ # In this case the +log_children+ gets executed before +do_something_else+.
+ # The same applies to all non-transactional callbacks.
+ #
+ # In case there are multiple transactional callbacks as seen below, the order
+ # is reversed.
+ #
+ # For example:
+ #
+ # class Topic
+ # has_many :children
+ #
+ # after_commit :log_children
+ # after_commit :do_something_else
+ #
+ # private
+ #
+ # def log_chidren
+ # # Child processing
+ # end
+ #
+ # def do_something_else
+ # # Something else
+ # end
+ # end
+ #
+ # In this case the +do_something_else+ gets executed before +log_children+.
+ #
# == \Transactions
#
# The entire callback chain of a {#save}[rdoc-ref:Persistence#save], {#save!}[rdoc-ref:Persistence#save!],