Say I have an assets model that contains two columns:
Assets
ownership:string
lease_id:integer # My Lease Object
So ownership can either be “OWN” or “LEASE”.
Now, I only want to allow a lease_id if ownership is LEASE and if ownership happens to be LEASE then I want to require a lease object.
How can this be done in Rails 3.2.2?
You can add optional validations based on a method which returns true/false. Keep in mind that this will only enforce that a
lease_idis present whenownership == "LEASE". This will not restrict alease_idfrom being added in any case.If you want to restrict a
lease_idaltogether, you can use a callback to remove the property before the object is saved to the DB.