2010-12-09 97 views

回答

7

@current_userinstance variableself.current_user调用返回该实例变量在第10行的方法中,第一填充它,如果它是目前NIL。

4

@current_user访问而self.current_user呼吁selfcurrent_user方法的对象的实际属性。

这意味着你可以做这样的事情:

def current_user 
    @current_user.first_name 
end 

所以现在访问@current_user仍然会给你的财产,但self.current_user会给你回的只有名字。

在你的具体的例子,他们正在使用对象缓存设置@current_user属性仅在第一次被访问。这意味着,如果@current_user是零,它会做(login_from_session || login_from_basic_auth || login_from_cookie)否则只会返回现有对象,而无需重新初始化。

1
@current_user 

取消引用名为@current_user的实例变量。

self.current_user 

将消息发送到:current_userself

相关问题