Skip to content

Commit cf2c6ea

Browse files
authored
Merge pull request rails#52047 from seanpdoyle/association-callbacks-docs
Improve Active Record Association Callbacks docs [ci skip]
2 parents af3c866 + dadc661 commit cf2c6ea

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,21 +379,43 @@ def association_instance_set(name, association)
379379
# after_add: :congratulate_client,
380380
# after_remove: :log_after_remove
381381
#
382-
# def congratulate_client(record)
382+
# def congratulate_client(client)
383383
# # ...
384384
# end
385385
#
386-
# def log_after_remove(record)
386+
# def log_after_remove(client)
387387
# # ...
388388
# end
389389
# end
390390
#
391+
# Callbacks can be defined in three ways:
392+
#
393+
# 1. A symbol that references a method defined on the class with the
394+
# associated collection. For example, <tt>after_add: :congratulate_client</tt>
395+
# invokes <tt>Firm#congratulate_client(client)</tt>.
396+
# 2. A callable with a signature that accepts both the record with the
397+
# associated collection and the record being added or removed. For
398+
# example, <tt>after_add: ->(firm, client) { ... }</tt>.
399+
# 3. An object that responds to the callback name. For example, passing
400+
# <tt>after_add: CallbackObject.new</tt> invokes <tt>CallbackObject#after_add(firm,
401+
# client)</tt>.
402+
#
391403
# It's possible to stack callbacks by passing them as an array. Example:
392404
#
405+
# class CallbackObject
406+
# def after_add(firm, client)
407+
# firm.log << "after_adding #{client.id}"
408+
# end
409+
# end
410+
#
393411
# class Firm < ActiveRecord::Base
394412
# has_many :clients,
395413
# dependent: :destroy,
396-
# after_add: [:congratulate_client, -> (firm, record) { firm.log << "after_adding#{record.id}" }],
414+
# after_add: [
415+
# :congratulate_client,
416+
# -> (firm, client) { firm.log << "after_adding #{client.id}" },
417+
# CallbackObject.new
418+
# ],
397419
# after_remove: :log_after_remove
398420
# end
399421
#
@@ -1251,6 +1273,14 @@ module ClassMethods
12511273
# persisted new records placed at the end.
12521274
# When set to +:nested_attributes_order+, the index is based on the record order received by
12531275
# nested attributes setter, when accepts_nested_attributes_for is used.
1276+
# [:before_add]
1277+
# Defines an {association callback}[rdoc-ref:Associations::ClassMethods@Association+callbacks] that gets triggered <b>before an object is added</b> to the association collection.
1278+
# [:after_add]
1279+
# Defines an {association callback}[rdoc-ref:Associations::ClassMethods@Association+callbacks] that gets triggered <b>after an object is added</b> to the association collection.
1280+
# [:before_remove]
1281+
# Defines an {association callback}[rdoc-ref:Associations::ClassMethods@Association+callbacks] that gets triggered <b>before an object is removed</b> from the association collection.
1282+
# [:after_remove]
1283+
# Defines an {association callback}[rdoc-ref:Associations::ClassMethods@Association+callbacks] that gets triggered <b>after an object is removed</b> from the association collection.
12541284
#
12551285
# Option examples:
12561286
# has_many :comments, -> { order("posted_on") }

0 commit comments

Comments
 (0)