title: Problem with has_password? method in railstutorial.org ( Chapter 7)
body: <p>I am having the exact same problem describe in <a href="https://stackoverflow.com/questions/5706433/michael-hartls-rails-3-tutorial-problem-with-has-password-method-in-chapter-7">this question</a>, but for a different reason, apparently.</p>
  
  <p><a href="http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-two#sec:implementing_has_password" rel="nofollow noreferrer">This</a> is the relevant section of the book.</p>
  
  <p>I've done it exactly as on the book, as far as I can tell, and my rspec tests are all working fine.</p>
  
  <p>My User model code is this:</p>
  
  <pre><code>require 'digest' #Needs digest to do password encryption
  
  
  class User &lt; ActiveRecord::Base
  
    attr_accessor :password
    attr_accessible :name, :email, :password, :password_confirmation
    validates :name, :presence =&gt; true,
              :length =&gt; { :maximum =&gt; 20 }
  
    email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  
    validates :email, :presence =&gt; true,
                      :format =&gt; { :with =&gt; email_regex },
                      :uniqueness =&gt; { :case_sensitive =&gt; false }
  
    validates :password, :presence =&gt; true,
                         :confirmation =&gt;true,
                         :length =&gt; { :within =&gt; 5..24 }
  
    before_save :encrypt_password
  
    def has_password?(submitted_password)
      encrypt_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
  </code></pre>
  
  <p>As I said, the tests are working fine, but has_password? always returns false in the console.
  This is what I did in the console:</p>
  
  <pre><code>ruby-1.9.2-p180 :002 &gt; User.create!(:name =&gt; 'userdude', :email =&gt; 'a@a.com', :password =&gt; 'foobar', :password_confirmation =&gt; 'foobar' )
  
  =&gt; #&lt;User id: 2, name: "userdude", email: "a@a.com", created_at: "2011-07-28 23:13:40", updated_at: "2011-07-28 23:13:40", encrypted_password: "84f9a1348af1f7e6610fb1d0c7002efe96f43dc97d807b3ac00...", salt: "13cf7bfd386af4fcf282ae5fa4831eb9d1dff15e842be8cba88..."&gt; 
  
  ruby-1.9.2-p180 :003 &gt; User.all
   =&gt; [#&lt;User id: 2, name: "userdude", email: "a@a.com", created_at: "2011-07-28 23:13:40", updated_at: "2011-07-28 23:13:40", encrypted_password: "84f9a1348af1f7e6610fb1d0c7002efe96f43dc97d807b3ac00...", salt: "13cf7bfd386af4fcf282ae5fa4831eb9d1dff15e842be8cba88..."&gt;] 
  
  ruby-1.9.2-p180 :004 &gt; user = User.first
   =&gt; #&lt;User id: 2, name: "userdude", email: "a@a.com", created_at: "2011-07-28 23:13:40", updated_at: "2011-07-28 23:13:40", encrypted_password: "84f9a1348af1f7e6610fb1d0c7002efe96f43dc97d807b3ac00...", salt: "13cf7bfd386af4fcf282ae5fa4831eb9d1dff15e842be8cba88..."&gt; 
  
  ruby-1.9.2-p180 :005 &gt; user
   =&gt; #&lt;User id: 2, name: "userdude", email: "a@a.com", created_at: "2011-07-28 23:13:40", updated_at: "2011-07-28 23:13:40", encrypted_password: "84f9a1348af1f7e6610fb1d0c7002efe96f43dc97d807b3ac00...", salt: "13cf7bfd386af4fcf282ae5fa4831eb9d1dff15e842be8cba88..."&gt; 
  
  ruby-1.9.2-p180 :006 &gt; user.has_password?('foobar')
   =&gt; false 
  ruby-1.9.2-p180 :007 &gt;
  </code></pre>
  
  <p>As you see, there are not any records already created, as in the other question I linked.</p>