ruby on rails - Removing a validation at runtime -


below can add validation @ runtime mongoid:

class abc   include mongoid::document   field :something, type: string end   = abc.new a.valid?  => true   abc.class_eval   validates_presence_of :something end  => [mongoid::validatable::presencevalidator]   b = abc.new => #<abc _id: 55948e466d616344a4010000, something: nil>  b.valid?  => false  

how remove validation? if possible, assume same both activerecord , mongoid.

i'm looking this:

abc.class_eval   remove_validates_presence_of :something end 

i think you'll find want in blog post: http://gistflow.com/posts/749-canceling-validations-in-activerecord

basically, validators information in class variable _validators, , can call skip_callback cancel it.
should able remove 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 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -