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

Merge pull request #35530 from Rodrigora/patch-1

Add note about has_many associations callbacks [ci skip]
This commit is contained in:
Vipul A M 2019-03-12 16:09:04 +05:30 committed by GitHub
commit 7ed1302cd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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: