From ec6089995d456c91aadf1cb2324b2e626016975c Mon Sep 17 00:00:00 2001 From: Fabian Schwahn Date: Tue, 18 Jul 2017 16:57:57 +0200 Subject: Improve documentation of Procs as :if / :unless options for callbacks --- guides/source/active_record_callbacks.md | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'guides') diff --git a/guides/source/active_record_callbacks.md b/guides/source/active_record_callbacks.md index 5b06ff78bb..5946acb412 100644 --- a/guides/source/active_record_callbacks.md +++ b/guides/source/active_record_callbacks.md @@ -319,6 +319,14 @@ class Order < ApplicationRecord end ``` +As the proc is evaluated in the context of the object, it is also possible to write this as: + +```ruby +class Order < ApplicationRecord + before_save :normalize_card_number, if: Proc.new { paid_with_card? } +end +``` + ### Multiple Conditions for Callbacks When writing conditional callbacks, it is possible to mix both `:if` and `:unless` in the same callback declaration: -- cgit v1.2.3