1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add combining callback conditions [skip ci] (#35313)

This commit is contained in:
Shodai Suzuki 2019-02-19 13:16:32 +09:00 committed by Ryuta Kamizono
parent ccaa6199a7
commit d285486032

View file

@ -338,6 +338,20 @@ class Comment < ApplicationRecord
end end
``` ```
### Combining Callback Conditions
When multiple conditions define whether or not a callback should happen, an `Array` can be used. Moreover, you can apply both `:if` and `:unless` to the same callback.
```ruby
class Comment < ApplicationRecord
after_create :send_email_to_author,
if: [Proc.new { |c| c.user.allow_send_email? }, :author_wants_emails?],
unless: Proc.new { |c| c.article.ignore_comments? }
end
```
The callback only runs when all the `:if` conditions and none of the `:unless` conditions are evaluated to `true`.
Callback Classes Callback Classes
---------------- ----------------