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 4334670
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:33:57+00:00 2026-05-21T10:33:57+00:00

This has been kicking my butt all morning. Right now, I’m just beginning Chapter

  • 0

This has been kicking my butt all morning.

Right now, I’m just beginning Chapter 7.2.4 of Michael Hartl’s excellent Ruby on Rails 3 Tutorial, and I’m running into some issues. The section begins with a quick check of the has_password? method, in the Rails console sandbox. Here’s what I’ve typed in:

ruby-1.9.2-p180 :001 > User
=> User(id: integer, name: string, email: string, created_at: datetime, updated
updated_at: datetime, encrypted_password: string, salt: string) 
ruby-1.9.2-p180 :002 > User.create(:name => "John Pavlick", :email => "jmpavlick
@gmail.com", :password => "foobar", :password_confirmation => "foobar")
 => #<User id: nil, name: "John Pavlick", email: "jmpavlick@gmail.com", created_
at: nil, updated_at: nil, encrypted_password: nil, salt: nil> 
ruby-1.9.2-p180 :003 > user = User.find_by_email("jmpavlick@gmail.com"
ruby-1.9.2-p180 :004?>   )
 => #<User id: 1, name: "John Pavlick", email: "jmpavlick@gmail.com", created_at
: "2011-04-15 15:11:46", updated_at: "2011-04-15 15:11:46", encrypted_password:
nil, salt: nil>
ruby-1.9.2-p180 :005 > user.has_password?("foobar")
 => false

This should be returning true.

Here’s my user.rb model:

class User < ActiveRecord::Base
    attr_accessor :password
    attr_accessible :name, :email, :password, :password_confirmation

    email_regex = /^[\w+\-.]+@[a-z\d\-.]+\.[a-z]+$/i

    validates :name, :presence => true,
                        :length => { :maximum => 50 }
    validates :email, :presence => true,
                        :format => { :with => email_regex },
                        :uniqueness => { :case_sensitive => false }

    # Automatically create the virtual attribute 'password confirmation'
    validates :password, :presence => true,
                                             :confirmation => true,
                                             :length => { :within => 6..40 }

    before_save :encrypt_password

    # Return true if the user's password matches the submitted password

    def has_password?(submitted_password)
        encrypted_password == encrypt(submitted_password)
      end

      private

        def encrypt_password
          self.salt = make_salt if new_record?
          self.encrypted_password = encrypt(password)
        end

        def encrypt(string)
          secure_hash("#{salt}--#{string}")
        end

        def make_salt
          secure_hash("#{Time.now.utc}--#{password}")
        end

        def secure_hash(string)
          Digest::SHA2.hexdigest(string)
        end
end

All of my RSpec tests pass perfectly, and as far as I can tell, I’ve coded in verbatim everything in the book – I’ve even copy/pasted some code to make sure. I don’t see any reason at all for this to be failing, so any help will be a lifesaver.

Here’s a link to the book, online: [link]

  • 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-21T10:33:58+00:00Added an answer on May 21, 2026 at 10:33 am

    After your create call, notice that the object returned does not have a id or encrypted password… I suspect that a validation is failing and the record is not being created, perhaps because you already have a record present?

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

Sidebar

Related Questions

This problem has been kicking my butt for a few days now. I have
The jqGrid has been kicking my butt (as well as others on this site).
This has been driving me crazy all day. I just want the value of
This has been frustrating me for a while now. I started developing a site
This has been bugging me for more than two days now, so i thought
I am embarrassed to ask this simple question, but has been in kicking my
This has been a head-banger for me. I have looked at all of the
This has been bugging me for years, but I've just been ignoring it, like
This has been bothering me for a long time now: Lets say i have
This has been bugging me for the last few hours now. I am using

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.