Ruby-on-rails Rails 中的默认 getter 和 setter 是什么样的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8774837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What would a default getter and setter look like in rails?
提问by jay
I know that I can write attr_accessor :tag_list to make a virtual attribute tag_list for an object in Rails. This allows there to be a tag_list attribute in forms for the object.
我知道我可以编写 attr_accessor :tag_list 来为 Rails 中的对象创建一个虚拟属性 tag_list。这允许对象的表单中有一个 tag_list 属性。
If I use attr_accessor :tag_list I can, in the model, perform actions on tag_list to pull and manipulate data from the form.
如果我使用 attr_accessor :tag_list 我可以在模型中对 tag_list 执行操作以从表单中提取和操作数据。
What I want to know is, instead of writing attr_accessor, how would I write a getter and setter that would replicate completely the default functionality of attr_accessor. EG:
我想知道的是,我将如何编写一个 getter 和 setter 来完全复制 attr_accessor 的默认功能,而不是编写 attr_accessor。例如:
def tag_list
#what goes here
end
FYI I have tried
仅供参考我试过
def tag_list
@tag_list
end
This does NOT work.
这不起作用。
回答by Niklas B.
attr_accessoris a built-in Ruby method and has no special meaning in the context ActiveRecord. attr_accessor :tag_listis basically equivalent to this code:
attr_accessor是一个内置的 Ruby 方法,在上下文 ActiveRecord 中没有特殊含义。attr_accessor :tag_list基本上相当于这个代码:
# getter
def tag_list
@tag_list
end
# setter
def tag_list=(val)
@tag_list = val
end
In ActiveRecord models, however, it could be that you want something like this:
然而,在 ActiveRecord 模型中,您可能想要这样的东西:
def tag_list
self[:tag_list]
end
def tag_list=(val)
self[:tag_list] = val
end
There is a slight difference: With the first method, obj[:tag_list]doesn't use the same storage as your getter and setter. With the latter, it does.
有一点不同:使用第一种方法,obj[:tag_list]不使用与 getter 和 setter 相同的存储。对于后者,确实如此。
Explanation of the getter/setter concept
getter/setter 概念的解释
In Ruby, the following two lines of code are equivalent
在Ruby中,下面两行代码是等价的
thing.blabla
thing.blabla()
Both call the method blablaof the object thingand evaluate to the last expression evaluated within that method. This means, you also don't need a returnstatement in the case of the above getter method, because the method simply returns the last expression in the method (@tag_list, the value of the instance variable).
两者都调用blabla对象的方法thing并求值到该方法中求值的最后一个表达式。这意味着,return在上述 getter 方法的情况下,您也不需要语句,因为该方法只返回方法中的最后一个表达式 ( @tag_list,实例变量的值)。
Also, those two lines of code are equivalent:
此外,这两行代码是等效的:
thing.blabla=("abc")
thing.blabla = "abc"
Both call the method blabla=of the object thing. The special name with the =character can be used like any other method name.
两者都调用blabla=对象的方法thing。带有=字符的特殊名称可以像任何其他方法名称一样使用。
The fact that attributes, as they are sometimes called, are in fact plain methods, you can also use some special logic transformed on the values before returning or accepting them. Example:
事实上,属性有时被称为,实际上是普通方法,您还可以在返回或接受值之前使用一些特殊的逻辑转换值。例子:
def price_in_dollar
@price_in_euro * 0.78597815
end
def price_in_dollar=(val)
@price_in_euro = val / 0.78597815
end
回答by leonardoborges
When using ActiveRecord, this is the equivalent getter and setter versions:
使用 ActiveRecord 时,这是等效的 getter 和 setter 版本:
def tag_list
read_attribute(:tag_list)
end
def tag_list=(val)
write_attribute(:tag_list, val)
end
Is this what you were looking for?
这就是你要找的吗?
回答by user3487016
Notice the code below is in the [Helpers] path. Helpers are now included for
all [Controllers] to work from when instantiated.
module SettergettersHelper
#TODO Wayne
mattr_accessor :nameport
#TODO Wayne Mattingly the code below was replaced BY ABOVE
#TODO and not depricatable RAILS 4.2.3
# def nameport
# @nameport
# end
# def nameport=(nameport)
# @nameport = nameport
#end
end
*Getter from Accounts Controller:*
def index
@portfolio_name = nameport
end
*Setter from Portfolio Controller:*
def show
@portfolio_name = @portfolio_name.portfolio_name #from database call
SettergettersHelper.nameport = @portfolio_name # set attribute
end

