Rails - before_save不工作?

Kva*_*ass 8 ruby callback ruby-on-rails-3

我正在关注Michael Hartl的RoR教程,它涵盖了密码加密的基础知识.这是目前的用户模型:

class User < ActiveRecord::Base
    attr_accessor :password

    attr_accessible :name, :email,: password, :password_confirmation

    email_regex = /^[A-Za-z0-9._+-]+@[A-Za-z0-9._-]+\.[A-Za-z0-9._-]+[A-Za-z]$/
                                              #tests for valid email addresses.

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

    before_save :encrypt_password

    private

        def encrypt_password
            @encrypted_password = encrypt(password)
        end

        def encrypt(string)
            string
        end
end
Run Code Online (Sandbox Code Playgroud)

(显然这不是在进行任何加密,因为加密方法并没有真正实现,但这不是我的问题)

然后我编写了以下规范(根据教程):

require 'spec_helper'

describe User do

    before(:each) do
        @attr = { :name => "Example User", :email => "user@example.com",
                  :password => "abc123", :password_confirmation => "abc123"}
    end

    describe "password encryption" do

        before(:each) do
            @user = User.create!(@attr) # we are going to need a valid user in order
                                        # for these tests to run.
        end

        it "should have an encrypted password attribute" do
            @user.should respond_to(:encrypted_password)
        end

        it "should set the encrypted password upon user creation" do
            @user.encrypted_password.should_not be_blank
        end

    end
end
Run Code Online (Sandbox Code Playgroud)

第一次测试通过,但由于@user.encrypted_password是零,第二次测试失败.但我不明白为什么它是零,因为该encrypt_password方法应该被调用before_save.我知道我必须遗漏一些东西 - 请有人解释一下吗?

小智 20

encrypt_password方法不正确,应该是:

def encrypt_password
  self.encrypted_password = encrypt(password)
end
Run Code Online (Sandbox Code Playgroud)

注意self的使用,它将正确设置用户对象的属性,而不是创建一个被遗忘的实例变量.