The dynamic find_by method does not work in the class

I have a user model in which there is an authentication method.

If I test using the model in the rails console, you can create a user just fine, and then I can find an email on it and return the user in the same way.

user = User.find_by_email("someaddress@email.com")

Now, if I try to call an authentication method like this and return the user, the puts operator in my authentication method returns nil

user = User.authenticate("someaddress@email.com", "foobar")

The model looks something like this:

  class User < ActiveRecord::Base

  attr_accessor   :password
  attr_accessible :first_name, :last_name,
                  :email, :birth_date, :sex,
                  :password, :password_confirmation


  def self.authenticate(email, submitted_password) 
    user = find_by_email(email) 

    puts user  #this returns nil so my class is never able to authenticate the user

    return nil if user.nil? 
    return user if user.has_password?(submitted_password)
  end

end

I do not understand what the problem is. Some of them see in this question.

+3
source share
4 answers

The way to use the find_by method inside the class method is good; which should work.

, nil puts? , , . , user.has_password? .

puts :

p user

... .

+2

find_by_email? , , sql dbconsole.

0

nil, :

  • (, find_by_email )
  • has_password? false, .

, has_password? .

0

, ... , - .

, , "" .

As for my initial question, it turns out that this method works. I had a typo in the string that I passed to the method. Mostly I left ".com" at the end of the letter.

As usual, a simple typo makes me feel dumb to post the question, but despite all my thoughts, although the problem and finding your suggestions helped me find a solution, thanks so much.

0
source

Source: https://habr.com/ru/post/1777355/


All Articles