How to include validates_confrmation_of in a module in rails

I want to turn on

validates_confirmation_of :password

      

in the module, but I keep getting errors like:

"undefined method` validates_confirmation_of 'for password :: ClassMethods: Module "

Not sure how I can get it to work.

thanks

+1


a source to share


1 answer


You cannot call validates_confirmation_of

in the module itself, because the module's code is executed when it is created. Instead, you want to call the validator method when the module is included in the ActiveRecord model, for example:



module Password::ClassMethods
  def self.included(base)
    base.send(:validates_confirmation_of, :password)
  end
end

      

+3


a source







All Articles