Ruby-on-rails 在 Rails 中使用 attr_accessor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2793098/
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
usage of attr_accessor in Rails
提问by Ethan
When do you use attr_reader/attr_writer/attr_accessorin Rails models?
当你使用attr_reader/ attr_writer/ attr_accessorRails中的模型?
回答by Peter
Never, unless you have specific need for it. Automatic database-backed accessors are created for you, so you don't need to worry.
永远不要,除非您有特殊需要。为您创建了自动数据库支持的访问器,因此您无需担心。
Any attr_accessors you do create will change the relevant @attron the rails object, but this will be lost when the object is destroyed, unless you stick it back in the database. Sometimes you do want this behavior, but it's unusual in a rails app.
attr_accessor您创建的任何s 都会更改@attrrails 对象上的相关内容,但是当对象被销毁时,这将丢失,除非您将其重新放入数据库中。有时您确实想要这种行为,但在 Rails 应用程序中并不常见。
Now in ruby, it's a different story, and you end up using these very frequently. But I'd be surprised if you need them in rails---especially initially.
现在在 ruby 中,这是一个不同的故事,你最终会非常频繁地使用这些。但是,如果您在 Rails 中需要它们,我会感到惊讶——尤其是最初。
回答by Noah Thorp
attr_accessor can be used for values you don't want to store in the database directly and that will only exist for the life of the object (e.g. passwords).
attr_accessor 可用于您不想直接存储在数据库中的值,并且仅在对象的生命周期内存在(例如密码)。
attr_reader can be used as one of several alternatives to doing something like this:
attr_reader 可以用作执行以下操作的几种替代方法之一:
def instance_value
"my value"
end
回答by brycemcd
Rails models are just ruby classes that inherit from ActiveRecord::Base. ActiveRecord employs attr_accessors to define getters and setters for the column names that refer to the ruby class's table. It's important to note that this is just for persistence; the models are still just ruby classes.
Rails 模型只是继承自ActiveRecord::Base. ActiveRecord 使用attr_accessors 为引用 ruby 类表的列名定义 getter 和 setter。重要的是要注意,这只是为了持久性;模型仍然只是 ruby 类。
attr_accessor :foois simply a shortcut for the following:
attr_accessor :foo只是以下内容的快捷方式:
def foo=(var)
@foo = var
end
def foo
@foo
end
attr_reader :foois simply a shortcut for the following:
attr_reader :foo只是以下内容的快捷方式:
def foo
@foo
end
attr_writer :foois a shortcut for the following:
attr_writer :foo是以下内容的快捷方式:
def foo=(var)
@foo = var
end
attr_accessoris a shortcut for the getter and setterwhile attr_readeris the shortcut for the getterand attr_writeris a shortcut for just the setter.
attr_accessor对于一个快捷方式getter和setter,而attr_reader对于快捷的getter和attr_writer是只是一个快捷方式二传手。
In rails, ActiveRecord uses these getters and setters in a convenient way to read and write values to the database. BUT, the database is just the persistence layer. You should be free to use attr_accessorand attr_readeras you would any other ruby class to properly compose your business logic. As you need to get and set attributes of your objects outside of what you need to persist to the database, use the attr_s accordingly.
在 Rails 中,ActiveRecord 以一种方便的方式使用这些 getter 和 setter 来读取和写入数据库中的值。但是,数据库只是持久层。你应该可以自由使用attr_accessor,并attr_reader为你的任何其他宝石类正确撰写你的业务逻辑。由于您需要获取和设置对象的属性,而不是需要持久化到数据库中的内容,因此请相应地使用attr_s。
More info:
更多信息:
http://apidock.com/ruby/Module/attr_accessor
http://apidock.com/ruby/Module/attr_accessor
http://www.rubyist.net/~slagell/ruby/accessors.html
http://www.rubyist.net/~slagel/ruby/accessors.html
回答by Benson Clark
If you are using it to validate the acceptance of the terms_of_service, you should really consider using validates :terms_of_service, :acceptance => true. It will create a virtual attribute and is much more concise.
如果您使用它来验证对 terms_of_service 的接受,您应该真正考虑使用 validates :terms_of_service, :acceptance => true。它将创建一个虚拟属性,并且更加简洁。
http://guides.rubyonrails.org/active_record_validations.html#acceptance.
http://guides.rubyonrails.org/active_record_validations.html#acceptance。
回答by lulalala
One example is to have a number of options stored in one serialized column. Form builder would complain if you try to have a text field for one of these options. You can use attr_accessor to fake it, and then in the update action save it in the serialized column.
一个示例是将多个选项存储在一个序列化列中。如果您尝试为这些选项之一设置文本字段,表单构建器会抱怨。您可以使用 attr_accessor 伪造它,然后在更新操作中将其保存在序列化列中。

