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…