skip certain validation method in Model

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

Leave a Comment