Skip to content

Commit 7ed1302

Browse files
authored
Merge pull request rails#35530 from Rodrigora/patch-1
Add note about has_many associations callbacks [ci skip]
2 parents c5979e7 + 7515afc commit 7ed1302

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)