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
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.