1

I'm looking to pass logic to a model when attempting to save in order to disable specific validations.

I know of other solutions such as this one: skip certain validation method in Model This involves putting a temporary attribute on a model which I want to avoid.

I'd rather that has a solution that ends up looking like this:

@my_model.save(:skip_name_validation => true)

^ Is something like this possible in Rails 3?

Community
  • 1
  • 1
nbb
  • 295
  • 3
  • 10

2 Answers2

3

For sure you can do this by performing a trick like:

class YourModel
  attr_accessor :skip_name_validation
  validate :validate_method, unless: :skip_name_validation
end

Now you can use it like:

@my_model.save(:skip_name_validation => true)
Hieu Pham
  • 6,577
  • 2
  • 30
  • 50
-1

Passing the attribute to #save as suggested in Hieu Pham's answer did not work for me.

I had to do it this way:

@my_model.skip_name_validation = true
@my_model.save

This implies the same setup as suggested previously, i.e. using attr_accessor to define the skip_name_validation attribute on the model.

bumcode
  • 362
  • 4
  • 13