Forum: Ruby on Rails Validation of co-dependent fields

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
86bf6a4d4103b1acbc6d23939ce57800?d=identicon&s=25 dhmspector (Guest)
on 2008-11-19 23:56
(Received via mailing list)
Is it possible to do validation on two fields that are co-dependent
inside a single validates_numericality_of statement?

Say for example I have two fields:  sales_headcount and
sales_compensation, in which both fields must be > 0 in order for
either to be valid (e.g., you can't be compensating sales people you
don't have...)  What I tried is:

 validates_numericality_of :sales_headcount, :greater_than => 0, :if
=> :sales_compensation, :greater_than => 0,  :message => "Sales
headcount can't be zero if compensation isn't zero "

validates_numericality_of : sales_compensation, :greater_than =>
0, :if => : sales_headcount, :greater_than =>  0,  :message => "Sales
compensation can't be zero if it's headcount isn't zero "

which causes a nil error to be thrown far away from the controller in
a view page  -- The API docs are not very rich with examples of how to
use the :if option or even if the :if open can be used to test another
field...   any pointers n the right formulation for this construct
would be appreciated..

This topic is locked and can not be replied to.