Rails-自我与@ [英] Rails -- self vs. @

查看:89
本文介绍了Rails-自我与@的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am following Michael Hartl's RoR tutorial, and it is covering the basics of password encryption. This is the User model as it currently stands:

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
        self.encrypted_password = encrypt(password)
    end

    def encrypt(string)
        string
    end
end

我之前发布了一个关于before_save无法正常工作的问题,结果发现我不小心做的事情是将我的crypto_password写为:

I posted a previous question about before_save not working, and it turns out that what I had accidentally done is written my encrypt_password as:

def encrypt_password
    @encrypted_password = encrypt(password)
end

我了解,如果self.encrypted_pa​​ssword设置了encryption_password属性,但是@encrypted_pa​​ssword为什么也不能这样做呢?在上一篇关于before_save无法正常工作的帖子中,有人说方法以我最初的编码方式结束后,实例变量被遗忘"了,为什么会这样呢?有人可以在上述代码的背景下解释self和@的工作方式有何不同吗?

I understand that if self.encrypted_password sets the encrypted_password attribute, but why does @encrypted_password not do that as well? In the response to the previous post about before_save not working someone said that the instance variable was "forgotten" after the method ended with the way I had originally coded it -- why was this the case? Can someone please explain how self and @ work differently in the context of the code above?

注意:我已经查看了帖子此处attribute =方法,我什至不知道该怎么做.由于我从未创建过该方法,也未声明该方法带有w attr_accessor,所以该方法可能在此处存在.因此,我仍然感到困惑,并且这不是对这些问题的重新发布.

NOTE: I already took a look at the posts here and here, but they both say that "self" is calling the attribute = method, and I don't even understand how that method could exist here since I never created it or declared the encrypted_password w/ attr_accessor. So I am still confused, and this is not a re-posting of those questions.

推荐答案

Rails会自动为您添加encrypted_password的访问器,因为users表中存在该名称的字段.

The accessors for encrypted_password have been automatically added by Rails for you because a field by that name exists in the users table.

您添加到表格中的任何字段都会通过self.field_name自动提供.

Any field you add to a table will be automatically made available via self.field_name.

这是Michael Hartl的教程在users表格.

还要查看链接页面中的user_spec.rb(清单7.3),作者正在其中测试encrypted_password字段的存在.

Also look at the user_spec.rb (Listing 7.3) in the linked page, where the author is testing for the presence of the encrypted_password field.

已更新:

正如@mu所指出的,@用于Ruby实例变量(又名"iv").但是encrypted_password是Rails定义的属性",不是实例变量.

As @mu points out, the @ is used for Ruby instance variables (aka "iv"). But encrypted_password is an "attribute" defined by Rails, and is not an instance variable.

如果运行User.find(1).instance_variables,您将看到存在一个名为@attributes的iv,其类型为Hash.

If you run User.find(1).instance_variables, you will see that there is an iv called @attributes, which is of type Hash.

在iv内是encrypted_password的存储位置. Rails为encrypted_password定义了访问器方法,该方法获取/设置该方法的数据. @attributes Hash中的属性.

Inside that iv is where the encrypted_password is stored. Rails has defined accessor methods for encrypted_password, which gets/sets the data for that attribute in the @attributes Hash.

请注意,您还可以通过在User类中调用的@attributes["encrypted_password"]获取/设置数据(但是访问器方法是实现此目的的便捷方法).

Note that you could also get/set the data via @attributes["encrypted_password"] called from within the User class (but the accessor methods are convenient way to do just that).

这篇关于Rails-自我与@的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆