# Validation of co-dependent fields

**URL:** <https://www.ruby-forum.com/t/validation-of-co-dependent-fields/153669>\
**Category:** Rails\
**Created:** [November 19, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/validation-of-co-dependent-fields/153669 "2008-11-19T23:56:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhmspector](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6a8cbe/32.png) [@dhmspector](https://www.ruby-forum.com/u/dhmspector)\
**Post date:** [November 19, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/validation-of-co-dependent-fields/153669/1 "2008-11-19T23:56:21Z")

</div>

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…

\_DHMS
