How to remove validation using instance_eval clause in Rails?

后端 未结 18 1888
情话喂你
情话喂你 2021-02-01 02:11

I would like to enhance existing class using instance_eval. There original definition contains validation, which require presence of certain fields, ie:

class Du         


        
18条回答
  •  天涯浪人
    2021-02-01 02:56

    If you doesn't want to make any changes in Parent class then first clear all validations in child class and copy all required validation from parent class to child class

      class Dummy < ActiveRecord::Base
          validates :property, presence: true
          validates :value, length: { maximum: 255 }
      end
    

    And override it in child class

    Dummy.class_eval do    
      clear_validators!
      validates :property, presence: true
    end
    

提交回复
热议问题