未验证 提交 27cf6ece 编写于 作者: J Jonathan Hefner 提交者: GitHub

Merge pull request #39193 from p8/guides/association-callbacks-with-throw

Association callbacks work with abort instead of exceptions [ci skip]
......@@ -2431,8 +2431,16 @@ class Author < ApplicationRecord
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.
If a `before_add` callback throws `:abort`, the object does not get added to
the collection. Similarly, if a `before_remove` callback throws `:abort`, the
object does not get removed from the collection:
```ruby
# book won't be added if the limit has been reached
def check_credit_limit(book)
throw(:abort) if limit_reached?
end
```
NOTE: These callbacks are called only when the associated objects are added or removed through the association collection:
```ruby
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册