skip certain validation method in Model
Asked Answered
E

6

41

I am using Rails v2.3

If I have a model:

class car < ActiveRecord::Base

  validate :method_1, :method_2, :method_3

  ...
  # custom validation methods
  def method_1
    ...
  end

  def method_2
    ...
  end

  def method_3
    ...
  end
end

As you see above, I have 3 custom validation methods, and I use them for model validation.

If I have another method in this model class which save an new instance of the model like following:

# "flag" here is NOT a DB based attribute
def save_special_car flag
   new_car=Car.new(...)

   new_car.save #how to skip validation method_2 if flag==true
end

I would like to skip the validation of method_2 in this particular method for saving new car, how to skip the certain validation method?

Educate answered 16/1, 2012 at 14:54 Comment(4)
Does your 'special car' have any attribute that can differentiate it from non-special cars? That would make this simple.Dmso
Have you gone over the conditional validation docs?Phira
Hi, I updated my post for the method to save a special car. Basically, i have a "flag" parameter pass to the method, and if "flag" is true, I would like to skip the validation of method_2Educate
@ Dave, I have gone over that. But what I want to achieve is in a method level, I am wondering is there any possibility to do it in method level, and how.Educate
B
74

Update your model to this

class Car < ActiveRecord::Base

  # depending on how you deal with mass-assignment
  # protection in newer Rails versions,
  # you might want to uncomment this line
  # 
  # attr_accessible :skip_method_2

  attr_accessor :skip_method_2 

  validate :method_1, :method_3
  validate :method_2, unless: :skip_method_2

  private # encapsulation is cool, so we are cool

    # custom validation methods
    def method_1
      # ...
    end

    def method_2
      # ...
    end

    def method_3
      # ...
    end
end

Then in your controller put:

def save_special_car
   new_car=Car.new(skip_method_2: true)
   new_car.save
end

If you're getting :flag via params variable in your controller, you can use

def save_special_car
   new_car=Car.new(skip_method_2: params[:flag].present?)
   new_car.save
end
Botswana answered 16/1, 2012 at 15:9 Comment(3)
That attr_writer should be attr_accessor or you'll get an undefined local variable or method 'skip_method' error. Or you could write your own reader function like skip_method?.Monkish
Thanks for this answer ..I searched a lot for this.For anyone else who might come across this ,I think you'll get a MassAssignment error if you do it like that . You can avoid the error by doing like so:new_car= Car.new , new_car.skip_method_2= params[:flag].present?Galcha
@lnreddy thanks for pointing out the mass assignment error. I've updated the answer to newer Rails versions.Botswana
D
19

The basic usage of conditional validation is:

class Car < ActiveRecord::Base

  validate :method_1
  validate :method_2, :if => :perform_validation?
  validate :method_3, :unless => :skip_validation?

  def perform_validation?
    # check some condition
  end

  def skip_validation?
    # check some condition
  end

  # ... actual validation methods omitted
end

Check out the docs for more details.

Adjusting it to your screnario:

class Car < ActiveRecord::Base

  validate :method_1, :method_3
  validate :method_2, :unless => :flag?

  attr_accessor :flag

  def flag?
    @flag
  end    

  # ... actual validation methods omitted
end

car = Car.new(...)
car.flag = true
car.save
Dahomey answered 16/1, 2012 at 15:5 Comment(1)
Actually, flag is not a DB based attribute...So...any other way?Educate
C
1

Another technique, which applies more to a migration script than application code, is to redefine the validation method to not do anything:

def save_special_car
   new_car=Car.new
   new_car.define_singleton_method(:method_2) {}
   new_car.save
end

#method_2 is now redefined to do nothing on the instance new_car.

Collapse answered 2/3, 2022 at 7:37 Comment(0)
M
0

Use block in your validation something like :

validates_presence_of :your_field, :if =>  lambda{|e| e.your_flag ...your condition}
Maryleemarylin answered 12/7, 2014 at 12:14 Comment(0)
H
0

With rails 6.x+ I think you can call .save or .valid? with a context attribute, like .save(:a_context) and, on the model side, use a validation like:

validate :method_2, on: :a_context

Ref: https://guides.rubyonrails.org/active_record_validations.html#on

Hither answered 10/4, 2023 at 23:47 Comment(0)
T
-5

Depending on weather flag is true of false, use the method save(false) to skip validation.

Trochal answered 16/1, 2012 at 15:9 Comment(3)
save(false) will skip entire validation, not just one method.Botswana
Also it is save(validate: false), not save(false)Caliber
it's save(false) in Rails v2.3Bouchard

© 2022 - 2024 — McMap. All rights reserved.