Using :unless with validates_presence_of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Require the contact details unless the listing uses a central contact # This works !! validates_presence_of :contact_name, :contact_phone, :contact_email, :if => :do_not_use_central_contact? # This doesn't !! validates_presence_of :contact_name, :contact_phone, :contact_email, :unless => :use_central_contact? # Check if the listing uses a central contact return false unless listing listing.use_central_contact? end # Check if the listing does not use a central contact. Just a handy method. # This is the same as !use_central_contact? but we can't use that in the model validations !use_central_contact? end |
