Вопрос

I'm totally confused. I'm learning Rails 4 and this returns user records as expected:

irb(main):002:0> User.all

But this returns nil

irb(main):004:0> User.authenticate('asdfdsf', 'asdfdas')

Here's the class method in the model:

def self.authenticate( email, password )
    user = User.all #( email: email )
    puts user
    # if user && (user.hashed_password == User.hash_with_salt( password, user.salt ) )
    #   return user
    # else
    #   return false
    # end
end

Am I missing something? Shouldn't these both return the same thing? Why can't I return a user object with .find() or .where() from inside a class method?

Update: Here's the schema of my users table:

  create_table "users", force: true do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "email"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "hashed_password"
    t.string   "salt"
  end
Это было полезно?

Решение

No. It would if there wasn't puts user line. puts call returns nil and it's returned from whole method as a last evaluated value.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top