diff options
author | Vipul A M <vipulnsward@gmail.com> | 2019-03-12 16:09:04 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-12 16:09:04 +0530 |
commit | 7ed1302cd2c458bb552fcd0ded83403c039de28d (patch) | |
tree | 858216835cd061b0f1a5c57397e3983d7576559f /guides/source | |
parent | c5979e7bea40f77a66c10169a1a3ced56d64241c (diff) | |
parent | 7515afcbbd22a639527ddba43876cc4763ceee2b (diff) | |
download | rails-7ed1302cd2c458bb552fcd0ded83403c039de28d.tar.gz rails-7ed1302cd2c458bb552fcd0ded83403c039de28d.tar.bz2 rails-7ed1302cd2c458bb552fcd0ded83403c039de28d.zip |
Merge pull request #35530 from Rodrigora/patch-1
Add note about has_many associations callbacks [ci skip]
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/association_basics.md | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index 672faad6f1..e076f10ece 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -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: |