Hey just remove attr_accessor :password
from model. It works
class User
include Mongoid::Document
attr_accessor :password//remove this line
field :name, type: String
field :category, type: String
field :email, type: String
field :password, type: String
validates :name, presence: true ,format: { with: /\A[a-zA-Z]+\z/}
validates :email, presence: true , format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i } , uniqueness: { case_sensitive: false }
validates :password , presence: true
before_save :downcase_fields
def downcase_fields
self.name.downcase!
self.category.downcase!
self.email.downcase!
end
end
field :password
will create setter and getter methods for that field which involves in .save
operation. But you are overriding mongoid
default getter method using attr_accessor :password
.
it will be like
def password
@password
end
here @password
is just a instance variable on User
model. it will be nil
always(because you are not setting any value to it). that’s why it was not working.
2
solved I am getting error while trying to login through valid email and password,which is stored in the database