user583726
user583726

Reputation: 685

can not access method after moving to Mongoid from ActiveRecord

I am trying to use Mongoid instead of SQLite for my app.

so, my code looks like as follows:

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

so,

Previously when I was using ActiveRecord, I was able to authenticate user by the following function :

 def authenticate(email, submitted_password)
-      user = find_by_email(email)
-      (user && user.has_password?(submitted_password)) ? user : nil

But now, authenticate function fails by saying that :

undefined method `has_password?' for #<Array:0xbcc55b4>

Am I missing any tiny detail for using Mongoid ?

Upvotes: 1

Views: 87

Answers (1)

abhas
abhas

Reputation: 5213

this line user = User.where(email:email_id) in Mongoid returns criterion which is an array so for selecting one user you have to replace it with user = User.where(email:email_id).first which will return only one document and on which you can run the method has_password?

Upvotes: 1

Related Questions