Weird problem I've been stuck on for several hours now:
I'm writing a simple user login using Rails 3, where a user logs in with their email and password. In my User model, I'm trying to find the correct User object by their email, then continue to see if they've entered the correct password.
When I try to find the User from the rails console
>> User.find_by_email("test@email.com")
I get the desired result - the correct User object is returned
User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test@email.com' LIMIT 1
=> #<User id: 21, first_name: "Test", last_name: "Man", hashed_password: "ca630762c6dce11af5a9923c7955131f8d6f7a16", cell_phone_number: nil, email: "test@email.com", created_at: "2012-12-13 14:35:38", updated_at: "2012-12-13 14:35:38", salt: nil, is_admin: false>
But when I put the following piece of code in the model/user.rb file, I get a weird result
user = User.find_by_email("test@email.com")
puts "******** user = #{user}"
Outputs the following:
******** user = #<User:0x10b7b82c8>
When I try to use this user variable, it errors out and says that the variable or method "user" is unknown
Here is the the output from the log file:
Started POST "/users/login_attempt" for 127.0.0.1 at Thu Dec 13 08:40:47 -0600 2012
Processing by UsersController#login_attempt as HTML
Parameters: {"password"=>"[FILTERED]", "utf8"=>"", "email"=>"asdf", "authenticity_token"=>"4xeLxpSb4GidsHUIe4b55LhOVJtDEiv5UaQYU9aBv5k=", "commit"=>"Log In"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test@email.com' LIMIT 1
Here's a few things that I've tried...
the the mysql query above definitely works. I've copied it into mysql and it got the correct user back.
I've tried find(id) which also doesn't work. Returns a similar User:0x10b7b82c8
Overall, I can't understand why the console would give a different result than the model code itself.
Any have any idea why I'm getting such a strange result returned from find_by?
Thanks a lot!
Edit: Here's additional code since Frederick mentioned there wasn't enough to find the problem.
From controllers/users_controller.rb
class UsersController < ApplicationController
def login
end
def login_attempt
authorized_user = User.authenticate(params[:email], params[:password])
redirect_to({:controller => 'records', :action => 'list'} )
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to({:controller => 'records', :action => 'list'} )
else
render('new')
end
end
end
from models/user.rb
require 'digest/sha1'
class User < ActiveRecord::Base
attr_accessible :password, :password_confirmation, :email, :email_confirmation, :first_name, :last_name
attr_accessor :password
before_save :create_hashed_password
after_save :clear_password
# only on create, so other attributes of this user can be changed
#create method to authenticate login credentials
def self.authenticate(email,password)
user = User.find_by_email("test@email.com")
puts "******** user = #{user}"
end
#method to check provided password against password stored in db
def password_match?(password="")
newuser.hashed_password == hash_password(password)
end
#hash_password adds salt to password and encrypts it
def self.hash_password(password="")
salted_pw = password + PW_SALT
hashed_pw = Digest::SHA1.hexdigest(salted_pw)
end
private
def create_hashed_password
# Whenever :password has a value hashing is needed
unless password.blank?
# self.hashed_password = User.hash_password(password)
self.hashed_password = User.hash_password(password)
end
end
def clear_password
# for security and b/c hashing is not needed
self.password = nil
end
end