我正在尝试为我的应用程序使用 Mongoid 而不是 SQLite。
所以,我的代码如下所示:
class User
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :email, type: String
field :encrypted_password, type: String
field :salt , type: String
field :admin , type: Boolean
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
.....
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
....
class << self
def authenticate(email_id, submitted_password)
print "authenticate the user " + email_id
user = User.where(email:email_id)
if user.nil?
return false
else
print "\n Check the passsword" + user.has_password?(submitted_password)
end
end
所以,
以前当我使用 ActiveRecord 时,我可以通过以下功能对用户进行身份验证:
def authenticate(email, submitted_password)
- user = find_by_email(email)
- (user && user.has_password?(submitted_password)) ? user : nil
但是现在,验证功能失败了:
undefined method `has_password?' for #<Array:0xbcc55b4>
我错过了使用 Mongoid 的任何微小细节吗?