[英]Why can't I directly access instance variables in Active Record callback?
在 Active Record 回调中,我总是看到使用self.variable
示例。 有时, self.
不需要前缀,所以他们只使用variable
(见这个相关问题)。
从我读过的内容来看,这两种引用类实例变量的方法是使用访问器函数,而@variable
将直接访问该变量。 在我的代码中,我尝试使用@variable
但它不起作用 - 就好像@variable
尚未定义......你不能直接在 Active Record 回调中访问实例变量吗?
另外,我还没有为这个特定变量定义访问器函数,所以当我说self.variable
或variable
时实际上会发生什么(我已经尝试了这两种方法,除了赋值的 LHS 之外,两者都可以工作)?
这个问题本质上就是我要问的,但我不明白答案并希望得到更多说明(我无法对答案发表评论,因为我是 Stack Overflow 的新手)。 他的回答是这样的:
它 [password] 与 self.password 相同。 ActiveRecord 定义方法,而不是实例变量来访问与数据库相关的内容。 这就是您无法使用@password 访问它的原因。
但是我看到的回调方法总是在模型的类中定义,这应该让他们可以访问实例变量,对吗? 他还这样说:
PS:回调只是对象上的方法调用。 唯一的区别是 Rails 为您而不是您自己调用它们。
那么他是否可能意味着回调方法不是从对对象的引用调用的? 例如,它不是作为model_instance.callback_method
调用,而是作为callback_method
? 如果 callback_method 是类实例方法,如何在不引用类实例的情况下找到它? 即使它确实以这种方式工作,它又如何知道self.variable
是什么?
不能直接在 Active Record 回调中访问实例变量吗?
您可以从任何实例方法访问实例变量,包括用作 ActiveRecord 回调的那些。
另外,我还没有为这个特定的变量定义访问器函数,所以当我说 self.variable 或 variable 时实际上会发生什么(我已经尝试了这两种方法,除了赋值的 LHS 之外,两者都可以工作)?
首先,了解这两种语法之间的区别很重要。 variable = 123
会将值123
分配给局部变量。 self.variable = 123
将在 self 上调用名为variable=
的方法并将123
作为参数传递。
> class Foo
> def x=(value)
> puts "Foo#x called with #{ value }"
> end
>
> def bar
> x = 123 # local variable assignment, `x` only exists inside the `bar` method.
> self.x = 456 # calls `x=`
> end
> end
> Foo.new.bar
Foo#x called with 456
有点令人困惑的是,使用隐式接收器调用的方法与引用局部变量共享相同的语法。
> class Bar
> def x
> "In method x"
> end
>
> def foo
> # No local variable `x` has been initialized yet, so references to `x` will call the method with that name.
> puts x # calls method `x` and prints "In method x".
> # This example is equivalent to `puts self.x`.
>
> x = "Hello" # Now that a local variable is defined it will hide the method `x`.
> puts x # references the local variable and prints "Hello".
> puts self.x # calls the method `x` and prints "In method x".
> end
> end
> Bar.new.foo
In method x
Hello
In method x
其次,您需要知道 ActiveRecord 为模型表的每一列创建访问器方法。 假设我有一个模型User(id: integer, name: string, age: integer)
。 ActiveRecord 将定义实例方法id
、 name
和age
来读取属性,并定义id=
、 name=
和age=
来设置属性。
实例变量完全是另一回事。 您无法使用@id
、 @name
、 @age
等实例变量访问数据库属性。
那么他是否可能意味着回调方法不是从对对象的引用调用的?
不,ActiveRecord 回调确实会调用对象上的实例方法。
我相信您不太了解 Rails 属性不存储在实例变量中。 您的所有属性(由数据库表模式定义)都存储在一个实例变量@attributes
,该变量永远不会被直接访问,因为它是 Rails 实现细节的一部分。
例子:
class Thing < ActiveRecord::Base
# assume that we have a 'name' column in the database
def foo
# correct ways to access the name
name
self.name
self[:name]
# this contains name value and other name metadata
# but you shouldn't use it
@attributes[:name]
# this holds nil as that variable has no relation to the "name" attribute
@name
end
end
在回调中,它的工作方式与在所有其他方法中相同——您可以访问您定义的实例变量,但不能将数据库属性作为实例变量访问。
class Thing < ActiveRecord::Base
attr_accessor :secret
after_initialize do
@secret = '123'
end
before_create do
p @secret
end
after_validation :on_validation
def on_validation
p @secret
end
end
如果你尝试创建一个Thing
的对象都基于块的before_create
回调和方法为基础after_validation
回调可以访问实例变量@secret
在定义after_initialize
回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.