I have a table with a couple of boolean columns.
A PurchaseOrder needs to be marked as complete (first boolean), before it is invoiced (the second boolean).
I'd appreciate some pointers as to how to validate legal combinations of these booleans.
What I have in mind is so开发者_运维百科mething like,
:validates (!:complete and !:invoiced) or (:complete && :invoiced) or (:complete && !:invoiced)
Is this possible?
Use a custom validator
Sounds like your PurchaseOrder goes through several states during its life cycle, like a finite-state machine.
One thing we use in production systems is acts_as_state_machine, to help accomplish this behavior.
It allows you to define:
- a series of states that an object can be in
- the events that move the object between states
- and finally which states you can move between, and which states you can't move between. For example, you might want to allow your PurchaseOrder to be go from complete->to->invoiced, but not backwards from invoiced->to->complete. acts_as_state_machine allows you to set this up in a declarative style.
acts_as_state_machine will allow you to define those complex behaviors. Reading it takes a bit of time to understand it, but for systems like these, it has been a life saver.
精彩评论