[英]usage of attr_accessor in Rails
什么attr_accessor
在Rails模型中使用attr_reader
/ attr_writer
/ attr_accessor
?
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_accessor
s you do create will change the relevant @attr
on the rails object, but this will be lost when the object is destroyed, unless you stick it back in the database. 您创建的任何
attr_accessor
都将更改rails对象上的相关@attr
,但是当对象被销毁时,这将丢失,除非您将其粘贴回数据库中。 Sometimes you do want this behavior, but it's unusual in a rails app. 有时你确实想要这种行为,但在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.
但是如果你需要它们在轨道上我会感到惊讶 - 特别是最初。
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 (eg 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
Rails models are just ruby classes that inherit from ActiveRecord::Base
. Rails模型只是继承自
ActiveRecord::Base
ruby类。 ActiveRecord employs attr_accessor
s to define getters and setters for the column names that refer to the ruby class's table. ActiveRecord使用
attr_accessor
来定义引用ruby类表的列名的getter和setter。 It's important to note that this is just for persistence; 重要的是要注意这只是为了坚持; the models are still just ruby classes.
这些模型仍然只是红宝石类。
attr_accessor :foo
is simply a shortcut for the following: attr_accessor :foo
只是以下的快捷方式:
def foo=(var)
@foo = var
end
def foo
@foo
end
attr_reader :foo
is simply a shortcut for the following: attr_reader :foo
只是以下的快捷方式:
def foo
@foo
end
attr_writer :foo
is a shortcut for the following: attr_writer :foo
是以下的快捷方式:
def foo=(var)
@foo = var
end
attr_accessor
is a shortcut for the getter and setter while attr_reader
is the shortcut for the getter and attr_writer
is a shortcut for just the setter . attr_accessor
是getter和setter的快捷方式,而attr_reader
是getter的快捷方式, attr_writer
只是setter的快捷方式。
In rails, ActiveRecord uses these getters and setters in a convenient way to read and write values to the database. 在rails中,ActiveRecord以方便的方式使用这些getter和setter来读取和写入数据库的值。 BUT, the database is just the persistence layer.
但是,数据库只是持久层。 You should be free to use
attr_accessor
and attr_reader
as you would any other ruby class to properly compose your business logic. 您可以像使用任何其他ruby类一样自由地使用
attr_accessor
和attr_reader
来正确attr_reader
业务逻辑。 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. 由于您需要在需要持久保存到数据库之外获取和设置对象的属性,因此请相应地使用
attr_
。
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/~slagell/ruby/accessors.html
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. 如果您使用它来验证terms_of_service的接受度,那么您应该考虑使用validates:terms_of_service,:acceptance => true。 It will create a virtual attribute and is much more concise.
它将创建一个虚拟属性,并且更加简洁。
http://guides.rubyonrails.org/active_record_validations.html#acceptance . http://guides.rubyonrails.org/active_record_validations.html#acceptance 。
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伪造它,然后在更新操作中将其保存在序列化列中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.