为什么在 ruby​​/rails/activerecord 中并不总是需要 self ? [英] Why isn't self always needed in ruby / rails / activerecord?

查看:34
本文介绍了为什么在 ruby​​/rails/activerecord 中并不总是需要 self ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 模型中测试 getter/setter 对时,我发现了一个很好的行为示例,我一直认为这些行为很奇怪且不一致.

在这个例子中,我正在处理 class Folder <ActiveRecord::Base.

文件夹属于_to :parent, :class_name =>'文件夹'

在getter方法上,如果我使用:

def parent_name父母名字结尾

...或...

def parent_name自.父.名结尾

...结果完全一样,我得到了父文件夹的名称.但是,在 getter 方法中,如果我使用...

def parent_name=(name)parent = self.class.find_by_name(name)结尾

... parent 变为 nil,但如果我使用...

def parent_name=(name)self.parent = self.class.find_by_name(name)结尾

...然后就可以了.

那么,我的问题是,为什么有时需要声明 self.method 而为什么可以只使用局部变量?

似乎ActiveRecord中self的需要/使用是不一致的,我想更好地理解这一点,所以我不觉得我总是在猜测是否需要声明自己与否.什么时候应该/不应该在 ActiveRecord 模型中使用 self?

解决方案

这是因为属性/关联实际上是方法(getter/setter)而不是局部变量.当您声明parent = value"时,Ruby 假定您要将值分配给局部变量 parent.

在堆栈的某处有一个 setter 方法def parent=",要调用它,您必须使用self.parent ="来告诉 ruby​​ 您实际上想要调用一个 setter 而不仅仅是设置一个局部变量.

当涉及到 getter 时,Ruby 首先查看是否有局部变量,如果找不到,则它会尝试找到具有相同名称的方法,这就是为什么您的 getter 方法在没有self"的情况下也能工作.

>

换句话说,这不是 Rails 的错,而是 Ruby 固有的工作方式.

希望有所帮助.

In testing a getter/setter pair in a rails model, I've found a good example of behavior I've always thought was odd and inconsistent.

In this example I'm dealing with class Folder < ActiveRecord::Base.

Folder belongs_to :parent, :class_name => 'Folder'

On the getter method, if I use:

def parent_name
  parent.name
end

...or...

def parent_name
  self.parent.name
end

...the result is exactly the same, I get the name of the parent folder. However, in the getter method if I use...

def parent_name=(name)
  parent = self.class.find_by_name(name)
end

... parent becomes nil, but if I use...

def parent_name=(name)
  self.parent = self.class.find_by_name(name)
end

...then then it works.

So, my question is, why do you need to declare self.method sometimes and why can you just use a local variable?

It seems the need for / use of self in ActiveRecord is inconsistent, and I'd like to understand this better so I don't feel like I'm always guessing whether I need to declare self or not. When should you / should you not use self in ActiveRecord models?

解决方案

This is because attributes/associations are actually methods(getters/setters) and not local variables. When you state "parent = value" Ruby assumes you want to assign the value to the local variable parent.

Somewhere up the stack there's a setter method "def parent=" and to call that you must use "self.parent = " to tell ruby that you actually want to call a setter and not just set a local variable.

When it comes to getters Ruby looks to see if there's a local variable first and if can't find it then it tries to find a method with the same name which is why your getter method works without "self".

In other words it's not the fault of Rails, but it's how Ruby works inherently.

Hope that helps.

这篇关于为什么在 ruby​​/rails/activerecord 中并不总是需要 self ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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