Removing a Runtime Check

Below I can successfully add runtime validation on Mongoid:

class Abc
  include Mongoid::Document
  field :something, type: String
end

 a = Abc.new
a.valid?
 => true 

Abc.class_eval do
  validates_presence_of :something
end
 => [Mongoid::Validatable::PresenceValidator] 
 b = Abc.new
=> #<Abc _id: 55948e466d616344a4010000, something: nil> 
b.valid?
 => false 

      

How do I remove a check? If possible, I assume it will be the same for both ActiveRecord and Mongoid.

I'm looking for something like this:

Abc.class_eval do
  remove_validates_presence_of :something
end

      

+3


source to share


1 answer


I think you will find what you want on this blog: http://gistflow.com/posts/749-canceling-validations-in-activerecord

Basically, the information about the validators is in a class variable _validators

and you can call skip_callback

to discard it.
Therefore, you can remove it with



validators = Abc._validators[:something] 
v = validators.first
validators.delete v
filter = Abc._validate_callbacks.find { |c| c.raw_filter == v }.filter
skip_callback :validate, filter

      

+2


source







All Articles