Cameron
Cameron

Reputation: 4201

How do I include a validates_confrmation_of in a Module in rails

I want to include

validates_confirmation_of :password

in a module but i keep getting errors like:

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

Not sure how I can make it work.

thanks

Upvotes: 1

Views: 94

Answers (1)

Bradford Fults
Bradford Fults

Reputation: 90

You can't 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 validation method when the module is included in the ActiveRecord model, like so:

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

Upvotes: 3

Related Questions