I have an invoice model with approver_note, po_number and state_id.
I need validations to check:
validates :approver_note, :presence => true, {:scope => state_id == 3}
validates :po_number, :presence => true, {:scope => state_id ==2}
So, if the user selects state_id = 3, he must enter a note.
If he selects state_id = 2, he must enter a po_number.
Any assistance would be great… thanks!
You’re looking for the
:ifoption instead of:scope.But since a lambda is a little ugly, I’d probably add a method to encapsulate what you’re doing a bit better:
If you actually have a bunch of different attributes that are required when
state_idis 3, not just a note, then you may want something like this:(Replace “green” with — I dunno — “high_documentation” or whatever makes sense in your world.)
Or maybe you want to let the state decide what it is:
It really does help to make the terminology in your code adhere more closely to your real-world language, as opposed to “3” and “2”.