1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add note about has_many associations callbacks [ci skip]

Add a note explaining when the has_many associations callbacks will be called or not.
This commit is contained in:
Rodrigo 2019-03-08 11:44:01 +00:00 committed by rodrigovrm
parent 199de6bee2
commit 7515afcbbd

View file

@ -2350,6 +2350,17 @@ end
If a `before_add` callback throws an exception, the object does not get added to the collection. Similarly, if a `before_remove` callback throws an exception, the object does not get removed from the collection.
NOTE: These callbacks are called only when the associated objects are added or removed through the association collection:
```ruby
# Triggers `before_add` callback
author.books << book
author.books = [book, book2]
# Does not trigger the `before_add` callback
book.update(author_id: 1)
```
### Association Extensions
You're not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example: