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

  • SEARCH
  • Home
  • 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 6114775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:00:52+00:00 2026-05-23T15:00:52+00:00

I’m following Iteration 13 of Agile Web development, for User login. I create a

  • 0

I’m following Iteration 13 of Agile Web development, for User login.

I create a migration to add 2 columns to my User model : hashed_password and salt.

  • Creation of Users works
  • login fails, due to this error undefined method ‘hashed_password’ in method ‘authenticate’

The problem is that :

  • In rails console, I can fetch User.first.hashed_password, what seems OK 🙂
  • I outputted the User that I fecth, and it is NOT nil
  • I tried to output user.hashed_password as I did in the rails console, but that throws always the same error :

NoMethodError (undefined method hashed_password' for #<ActiveRecord::Relation:0x00000003e6c3c0>):
app/models/user.rb:21:in
authenticate’
app/controllers/sessions_controller.rb:6:in `create’

This is my User model :

require 'digest/sha2'

class User < ActiveRecord::Base
has_and_belongs_to_many :products
has_many :created_products, :class_name => "Product", :foreign_key => :product_id

default_scope :order => "username ASC"


# Attributs pour le login (Livre)
validates :username, :presence => true, :uniqueness => true
validates :password, :confirmation => true
attr_accessor :password_confirmation
attr_reader :password
validate :password_must_be_present

def User.authenticate(name, password)
    logger.debug "---------- Beginning of Authenticate"
    if user = User.where(:username => name)

      logger.debug "utilisateur = #{user.inspect}"   # THIS IS OK AND NOT NIL
      logger.debug "utilisateur hashed PW = #{user.hashed_password}" # ERROR


        if user.hashed_password == encrypt_password(password, user.salt)
            return user
        end
    end
end

def User.encrypt_password(password, salt)
    Digest::SHA2.hexdigest(password + "wibble" + salt)
end

def password=(password)
    @password = password
    if (password.present?)
        generate_salt
        self.hashed_password = self.class.encrypt_password(password, salt)
    end
end


private

    def password_must_be_present
        errors.add(:password, "Mot de passe manquant") unless hashed_password.present?
    end

    def generate_salt
        self.salt = self.object_id.to_s + rand.to_s
    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-05-23T15:00:53+00:00Added an answer on May 23, 2026 at 3:00 pm

    User.where(:username => name) is returning an ActiveRecord::Relation object (hence the error message you are seeing). Try changing your if statement to:

    if user = User.where(:username => name).first
    

    That will set take the first matching user, which will be an instance of User and so will have a hashed_password field. If no user matches, you’ll get nil.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.