From 7515afcbbd22a639527ddba43876cc4763ceee2b Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Fri, 8 Mar 2019 11:44:01 +0000 Subject: [PATCH] Add note about has_many associations callbacks [ci skip] Add a note explaining when the has_many associations callbacks will be called or not. --- guides/source/association_basics.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index 672faad6f16..e076f10ece4 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: