ruby-on-rails - 为什么我无法直接访问 Active Record 回调中的实例变量?

标签 ruby-on-rails activerecord callback instance-variables

在 Active Record 回调中,我总是看到 examples使用 self.variable 。有时,不需要 self. 前缀,因此它们仅使用 variable (请参阅此 related question )。

据我所知,这两种引用类实例变量的方法使用访问器函数,而@variable将直接访问该变量。在我的代码中,我尝试使用 @variable 但它不起作用 - 就好像 @variable 尚未定义......您不能访问实例变量吗直接在 Active Record 回调中?

此外,我还没有为这个特定变量定义访问器函数,所以当我说 self.variablevariable 时实际会发生什么(我已经尝试过这两种方法) ,并且两者都在作业的 LHS 之外工作)?

This question本质上就是我要问的,但我不明白答案,需要更多澄清(我无法对答案发表评论,因为我是 Stack Overflow 的新手)。他的回答是这样的:

It [password] is the same as self.password. ActiveRecord defines methods, not instance variables to access stuff related to the database. That's why you cannot access it with @password.

但是我看到的回调方法总是在模型的类中定义,这应该让它们能够访问实例变量,对吗?他还这样说:

PS: Callbacks are only method calls on your object. The only difference is that Rails calls them for you, not yourself.

那么他的意思是否可能是回调方法不是从对象的引用中调用的?例如。它不是作为 model_instance.callback_method 调用,而是作为 callback_method 调用?如果callback_method是类实例方法,那么在没有对类实例的引用的情况下如何找到callback_method?即使它确实以这种方式工作,它怎么知道 self.variable 是什么?

最佳答案

can you not access instance variables directly in an Active Record callback?

可以从任何实例方法访问实例变量,包括用作 ActiveRecord 回调的变量。

Also, I haven't defined an accessor function for this particular variable, so what actually happens when I say self.variable or variable (I've tried both, and both work aside from the LHS of an assignment)?

首先,了解两种语法之间的区别很重要。 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 将定义实例方法 idnameage 来读取属性和 id=name=age= 设置属性。

实例变量完全是另一回事。您无法使用@id@name@age等实例变量访问数据库属性。

So does he maybe mean that the callback methods aren't call from a reference to the object?

不,ActiveRecord 回调确实会调用对象上的实例方法。

关于ruby-on-rails - 为什么我无法直接访问 Active Record 回调中的实例变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33681188/

相关文章:

ruby-on-rails - 如何在 Rails 中使用 "self"关键字

javascript - Uncaught ReferenceError : Stripe is not defined - STRIPE ERROR

ruby-on-rails - 如何将 ActiveRecord 属性转换为数据库或从数据库转换?

sql - 复杂的 Rails SQL 查询

ruby-on-rails - rails : belongs_to association through a form not populating

mysql - 此 mysql 代码中的语法错误

postgresql - 将 JSON 列类型迁移到 HSTORE 列类型

java - MQTT 客户端向代理发布消息后未收到确认

javascript - 使用参数响应 useCallback

http - Webhooks 是一种风格/模式还是一种规范?