Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails conditional validation in model

I have a Rails 3.2.18 app where I'm trying to do some conditional validation on a model.

In the call model there are two fields :location_id (which is an association to a list of pre-defined locations) and :location_other (which is a text field where someone could type in a string or in this case an address).

What I want to be able to do is use validations when creating a call to where either the :location_id or :location_other is validated to be present.

I've read through the Rails validations guide and am a little confused. Was hoping someone could shed some light on how to do this easily with a conditional.

like image 912
nulltek Avatar asked Jul 08 '14 20:07

nulltek


People also ask

What is the difference between validate and validates in rails?

So remember folks, validates is for Rails validators (and custom validator classes ending with Validator if that's what you're into), and validate is for your custom validator methods.

How does validation work in Rails?

Rails validation defines valid states for each of your Active Record model classes. They are used to ensure that only valid details are entered into your database. Rails make it easy to add validations to your model classes and allows you to create your own validation methods as well.

What is ActiveRecord in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.


1 Answers

I believe this is what you're looking for:

class Call < ActiveRecord::Base
  validate :location_id_or_other

  def location_id_or_other
    if location_id.blank? && location_other.blank?
      errors.add(:location_other, 'needs to be present if location_id is not present')
    end
  end
end

location_id_or_other is a custom validation method that checks if location_id and location_other are blank. If they both are, then it adds a validation error. If the presence of location_id and location_other is an exclusive or, i.e. only one of the two can be present, not either, and not both, then you can make the following change to the if block in the method.

if location_id.blank? == location_other.blank?
  errors.add(:location_other, "must be present if location_id isn't, but can't be present if location_id is")
end

Alternate Solution

class Call < ActiveRecord::Base
  validates :location_id, presence: true, unless: :location_other
  validates :location_other, presence: true, unless: :location_id
end

This solution (only) works if the presence of location_id and location_other is an exclusive or.

Check out the Rails Validation Guide for more information.

like image 135
Joe Kennedy Avatar answered Oct 22 '22 23:10

Joe Kennedy