# Validates\_each, how to set options?

**URL:** <https://www.ruby-forum.com/t/validates-each-how-to-set-options/145848>\
**Category:** Rails\
**Created:** [August 14, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/validates-each-how-to-set-options/145848 "2008-08-14T14:46:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 14, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/validates-each-how-to-set-options/145848/1 "2008-08-14T14:46:11Z")

</div>

Hi

I wrote the following validation in my model (using Rails 2.1)

validates\_each :checker :do |record, attr, value|  
record.errors.add attr, :existing\_checkin\_out if value == 0  
end , :on =\> :update, :if =\> :checker\_required?

obviously I got an error as I don’t know where to write the :on  
and :if options … (I also tried many other way …)

1- how can I write them in validates\_each ?

2- is there any better validation procedure, on UPDATE only, for  
this ‘checker’ field (value is 0 or 1)

if the field is updated to 0 (changing the status to non-checker…)  
I want to add an error  
ONLY when checker state 1 is still mandatory (checker\_required? =\>  
true)  
(some planned or confirmed checkins or checkouts instances are still  
depending upon this user)

here is my checker\_required? proc

protected  
def checker\_required?  
checks = self.checkins + self.checkouts  
return false if checks.empty?  
checks.map {|c| c[:id] if c.planned? ||  
c.confirmed? }.compact.empty? ? false : true  
end

thanks for your lights …

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 14, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/validates-each-how-to-set-options/145848/2 "2008-08-14T15:05:31Z")

</div>

On 14 Aug 2008, at 13:45, Erwin wrote:

> and :if options … (I also tried many other way …)
> 
> 1- how can I write them in validates\_each ?
> 
> 2- is there any better validation procedure, on UPDATE only, for  
> this ‘checker’ field (value is 0 or 1)

You could make things a lot easier with

def validate\_on\_update  
errors.add … if checker == 0 && checker\_required?  
end

Fred

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 14, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/validates-each-how-to-set-options/145848/3 "2008-08-14T19:11:59Z")

</div>

Thanks Fred

I knew I was far from being right… you made my day

On 14 août, 15:04, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 14, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/validates-each-how-to-set-options/145848/4 "2008-08-14T20:04:14Z")

</div>

To still answer your original question about where to put the options  
if you were to use that validation one day:  
validates\_each :attribute, :some\_other\_attribute, :on =\> :update, :if  
=\> :some\_proc\_to\_check do |record, attr, value|  
record.errors.add “message”, some\_check  
end

Makes only sense to use validates\_each if you have more than one  
attribute to validate in the block.

Dirk.
