Skip to content

Commit b3bd645

Browse files
authored
Merge pull request rails#27294 from eavgerinos/doc-ar-callbacks-order
[documentation] ActiveRecord: Document order of Callbacks
2 parents 5eff7a9 + 8f90e87 commit b3bd645

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

activerecord/lib/active_record/callbacks.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,55 @@ module ActiveRecord
225225
#
226226
# This way, the +before_destroy+ gets executed before the <tt>dependent: :destroy</tt> is called, and the data is still available.
227227
#
228+
# Also, there are cases when you want several callbacks of the same type to
229+
# be executed in order.
230+
#
231+
# For example:
232+
#
233+
# class Topic
234+
# has_many :children
235+
#
236+
# after_save :log_children
237+
# after_save :do_something_else
238+
#
239+
# private
240+
#
241+
# def log_chidren
242+
# # Child processing
243+
# end
244+
#
245+
# def do_something_else
246+
# # Something else
247+
# end
248+
# end
249+
#
250+
# In this case the +log_children+ gets executed before +do_something_else+.
251+
# The same applies to all non-transactional callbacks.
252+
#
253+
# In case there are multiple transactional callbacks as seen below, the order
254+
# is reversed.
255+
#
256+
# For example:
257+
#
258+
# class Topic
259+
# has_many :children
260+
#
261+
# after_commit :log_children
262+
# after_commit :do_something_else
263+
#
264+
# private
265+
#
266+
# def log_chidren
267+
# # Child processing
268+
# end
269+
#
270+
# def do_something_else
271+
# # Something else
272+
# end
273+
# end
274+
#
275+
# In this case the +do_something_else+ gets executed before +log_children+.
276+
#
228277
# == \Transactions
229278
#
230279
# The entire callback chain of a {#save}[rdoc-ref:Persistence#save], {#save!}[rdoc-ref:Persistence#save!],

0 commit comments

Comments
 (0)