Skip to content

Commit c079b5f

Browse files
authored
Merge pull request rails#49907 from p8/guides/association-callbacks
Move the association callbacks section to the callbacks guides [ci-skip]
2 parents 7567010 + a904df9 commit c079b5f

File tree

2 files changed

+64
-40
lines changed

2 files changed

+64
-40
lines changed

guides/source/active_record_callbacks.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,69 @@ Article destroyed
374374
=> #<User id: 1>
375375
```
376376

377+
Association Callbacks
378+
---------------------
379+
380+
Association callbacks are similar to normal callbacks, but they are triggered by events in the life cycle of a collection. There are four available association callbacks:
381+
382+
* `before_add`
383+
* `after_add`
384+
* `before_remove`
385+
* `after_remove`
386+
387+
You define association callbacks by adding options to the association declaration. For example:
388+
389+
```ruby
390+
class Author < ApplicationRecord
391+
has_many :books, before_add: :check_credit_limit
392+
393+
def check_credit_limit(book)
394+
# ...
395+
end
396+
end
397+
```
398+
399+
Rails passes the object being added or removed to the callback.
400+
401+
You can stack callbacks on a single event by passing them as an array:
402+
403+
```ruby
404+
class Author < ApplicationRecord
405+
has_many :books,
406+
before_add: [:check_credit_limit, :calculate_shipping_charges]
407+
408+
def check_credit_limit(book)
409+
# ...
410+
end
411+
412+
def calculate_shipping_charges(book)
413+
# ...
414+
end
415+
end
416+
```
417+
418+
If a `before_add` callback throws `:abort`, the object does not get added to
419+
the collection. Similarly, if a `before_remove` callback throws `:abort`, the
420+
object does not get removed from the collection:
421+
422+
```ruby
423+
# book won't be added if the limit has been reached
424+
def check_credit_limit(book)
425+
throw(:abort) if limit_reached?
426+
end
427+
```
428+
429+
NOTE: These callbacks are called only when the associated objects are added or removed through the association collection:
430+
431+
```ruby
432+
# Triggers `before_add` callback
433+
author.books << book
434+
author.books = [book, book2]
435+
436+
# Does not trigger the `before_add` callback
437+
book.update(author_id: 1)
438+
```
439+
377440
Conditional Callbacks
378441
---------------------
379442

guides/source/association_basics.md

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,46 +2756,7 @@ class Author < ApplicationRecord
27562756
end
27572757
```
27582758

2759-
Rails passes the object being added or removed to the callback.
2760-
2761-
You can stack callbacks on a single event by passing them as an array:
2762-
2763-
```ruby
2764-
class Author < ApplicationRecord
2765-
has_many :books,
2766-
before_add: [:check_credit_limit, :calculate_shipping_charges]
2767-
2768-
def check_credit_limit(book)
2769-
# ...
2770-
end
2771-
2772-
def calculate_shipping_charges(book)
2773-
# ...
2774-
end
2775-
end
2776-
```
2777-
2778-
If a `before_add` callback throws `:abort`, the object does not get added to
2779-
the collection. Similarly, if a `before_remove` callback throws `:abort`, the
2780-
object does not get removed from the collection:
2781-
2782-
```ruby
2783-
# book won't be added if the limit has been reached
2784-
def check_credit_limit(book)
2785-
throw(:abort) if limit_reached?
2786-
end
2787-
```
2788-
2789-
NOTE: These callbacks are called only when the associated objects are added or removed through the association collection:
2790-
2791-
```ruby
2792-
# Triggers `before_add` callback
2793-
author.books << book
2794-
author.books = [book, book2]
2795-
2796-
# Does not trigger the `before_add` callback
2797-
book.update(author_id: 1)
2798-
```
2759+
Read more about association callbacks in the [Active Record Callbacks Guide](active_record_callbacks.html#association-callbacks)
27992760

28002761
### Association Extensions
28012762

0 commit comments

Comments
 (0)