Skip to content

Commit 7515afc

Browse files
Rodrigorarodrigovrm
authored andcommitted
Add note about has_many associations callbacks [ci skip]
Add a note explaining when the has_many associations callbacks will be called or not.
1 parent 199de6b commit 7515afc

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

guides/source/association_basics.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,6 +2350,17 @@ end
23502350

23512351
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.
23522352

2353+
NOTE: These callbacks are called only when the associated objects are added or removed through the association collection:
2354+
2355+
```ruby
2356+
# Triggers `before_add` callback
2357+
author.books << book
2358+
author.books = [book, book2]
2359+
2360+
# Does not trigger the `before_add` callback
2361+
book.update(author_id: 1)
2362+
```
2363+
23532364
### Association Extensions
23542365

23552366
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:

0 commit comments

Comments
 (0)