aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorRodrigo <rodrigorcomp@gmail.com>2019-03-08 11:44:01 +0000
committerrodrigovrm <rodrigo@vrmtech.co.uk>2019-03-12 10:28:31 +0000
commit7515afcbbd22a639527ddba43876cc4763ceee2b (patch)
tree46e99b8502550db15a799cb394197d7cadfd801e /guides
parent199de6bee261dd816b68c841c7775fdcd02b68d2 (diff)
downloadrails-7515afcbbd22a639527ddba43876cc4763ceee2b.tar.gz
rails-7515afcbbd22a639527ddba43876cc4763ceee2b.tar.bz2
rails-7515afcbbd22a639527ddba43876cc4763ceee2b.zip
Add note about has_many associations callbacks [ci skip]
Add a note explaining when the has_many associations callbacks will be called or not.
Diffstat (limited to 'guides')
-rw-r--r--guides/source/association_basics.md11
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: