diff options
author | Shodai Suzuki <info@soartec-lab.work> | 2019-02-19 13:16:32 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2019-02-19 13:16:32 +0900 |
commit | d28548603244347cf97aa7021c54ea1eff75c9c4 (patch) | |
tree | ba18aa61ee683cf5dc89beec8d6e03e141c405db /guides/source/active_record_callbacks.md | |
parent | ccaa6199a74906ccb45fadbe1ce6a7e1fdd3d0d1 (diff) | |
download | rails-d28548603244347cf97aa7021c54ea1eff75c9c4.tar.gz rails-d28548603244347cf97aa7021c54ea1eff75c9c4.tar.bz2 rails-d28548603244347cf97aa7021c54ea1eff75c9c4.zip |
Add combining callback conditions [skip ci] (#35313)
Diffstat (limited to 'guides/source/active_record_callbacks.md')
-rw-r--r-- | guides/source/active_record_callbacks.md | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/guides/source/active_record_callbacks.md b/guides/source/active_record_callbacks.md index ebdee446f9..4579b748df 100644 --- a/guides/source/active_record_callbacks.md +++ b/guides/source/active_record_callbacks.md @@ -338,6 +338,20 @@ class Comment < ApplicationRecord 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 ---------------- |