ActiveModel :: SecurePassword undefined method `password_digest = '

I am trying to use 3.1 ActiveModel :: SecurePassword rails by following http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword

and I end with a red light ...

user.rb

class User < ActiveRecord::Base
  has_secure_password
  validates :password, :presence => { :on => :create }
end

factory.rb

Factory.define :user do |f|
  f.email "foo@bar.com"
  f.password "foobar"
  f.password_confirmation { |u| u.password }  
end

spec_user.rb

describe User do
  it "should authenticate with matching username and password" do
    user = Factory(:user, :email => 'frank@gmail.com', :password => 'secret')
    User.authenticate('frank@gmail.com', 'secret').should == user
  end
end

and I get a red light ...

 Failure/Error: user = Factory(:user, :email => 'frank@gmail.com', :password => 'secret')
 NoMethodError:
   undefined method `password_digest=' for #<User:0xb383460>

and I thought it was rake db: migrate problem, and I was looking at c rails, but obviously password_digest is defined.

ruby-1.9.2-p180 :007 > a = User.new
 => #<User id: nil, email: nil, password_digest: nil, is_admin: nil, created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :008 > a.password_digest = 3
 => 3 
+2
source share
2 answers

I had the same problem and I found (it seems to me) the best solution in the following comment:

http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword#comment-281584959

, password_digest . password_digest =, , won'tr ..

+6

describe User do
  it "should authenticate with matching username and password" do
    user = Factory(:user, :email => 'frank@gmail.com', :password => 'secret')
    User.find_by_email('frank@gmail.com').try(:authenticate, 'secret').should == user
  end
end
+1

All Articles