Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8985353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:18:41+00:00 2026-06-15T21:18:41+00:00

Weird problem I’ve been stuck on for several hours now: I’m writing a simple

  • 0

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…

  1. the the mysql query above definitely works. I’ve copied it into mysql and it got the correct user back.

  2. 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:18:42+00:00Added an answer on June 15, 2026 at 9:18 pm

    A method returns the result of the last expression in it. In the case of your authenticate method that last expression is the call to puts, which always returns nil, so

    authorized_user = User.authenticate(params[:email], params[:password])
    

    is setting authorized_user to nil rather than the user retrieved from the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is kind of weird problem. I have been using HttpWebRequest for a long
I had this weird problem a little while ago and haven't been able to
i'm having a weird problem about copying with Xcopy. I'm using Windows Server 2008
weird problem or bug. I'm using the jQuery Form Plugin and it works fine
I have a very weird problem while trying to pass and SQL query using
I ran into a weird problem. I built a mysql database where the user
i'm having an weird problem, my project have a login page, that's working, but
I'm debugging a weird problem with two simlar search forms - when user types
Some weird problem is haunting me right now: One of my apps (not yet
I had a weird problem, our website has registration form coded using zend form,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.