Rails update_attributes 不保存?
是否有不保存记录的 update_attributes
替代方案?
所以我可以做类似的事情:
@car = Car.new(:make => 'GMC')
#other processing
@car.update_attributes(:model => 'Sierra', :year => "2012", :looks => "Super Sexy, wanna make love to it")
#other processing
@car.save
顺便说一句,我知道我可以 @car.model = 'Sierra'
,但我想在一行上全部更新它们。
Is there an alternative to update_attributes
that does not save the record?
So I could do something like:
@car = Car.new(:make => 'GMC')
#other processing
@car.update_attributes(:model => 'Sierra', :year => "2012", :looks => "Super Sexy, wanna make love to it")
#other processing
@car.save
BTW, I know I can @car.model = 'Sierra'
, but I want to update them all on one line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信您正在寻找的是
assign_attributes
。它与 update_attributes 基本相同,但不保存记录:
I believe what you are looking for is
assign_attributes
.It's basically the same as update_attributes but it doesn't save the record:
您可以使用
assign_attributes
或attributes=
(它们是相同的)更新方法备忘单(适用于 Rails 6):
update< /code> =
assign_attributes
+保存
attributes=
=assign_attributes
的别名update_attributes
=已弃用,update
的别名来源:
https://github.com/rails/rails/blob /master/activerecord/lib/active_record/persistence.rb
https://github.com/rails/rails/blob /master/activerecord/lib/active_record/attribute_assignment.rb
另一个备忘单:
http://www.davidverhasselt.com/set-attributes-in- activerecord/#cheat-sheet
You can use
assign_attributes
orattributes=
(they're the same)Update methods cheat sheet (for Rails 6):
update
=assign_attributes
+save
attributes=
= alias ofassign_attributes
update_attributes
= deprecated, alias ofupdate
Source:
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/persistence.rb
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_assignment.rb
Another cheat sheet:
http://www.davidverhasselt.com/set-attributes-in-activerecord/#cheat-sheet
您可以使用“属性”方法:
来源:http://api.rubyonrails.org/类/ActiveRecord/Base.html
attributes=(new_attributes,guard_protected_attributes = true)
允许您通过传递带有与属性名称匹配的键的哈希值(再次与列名称匹配)来一次性设置所有属性。
如果guard_protected_attributes为true(默认值),则可以使用attr_protected宏来保护敏感属性免受这种形式的批量分配。或者,您也可以使用 attr_accessible 宏指定可以访问哪些属性。那么所有未包含在其中的属性将不允许被批量分配。
You can use the 'attributes' method:
Source: http://api.rubyonrails.org/classes/ActiveRecord/Base.html
attributes=(new_attributes, guard_protected_attributes = true)
Allows you to set all the attributes at once by passing in a hash with keys matching the attribute names (which again matches the column names).
If guard_protected_attributes is true (the default), then sensitive attributes can be protected from this form of mass-assignment by using the attr_protected macro. Or you can alternatively specify which attributes can be accessed with the attr_accessible macro. Then all the attributes not included in that won’t be allowed to be mass-assigned.
要向 ActiveRecord 模型批量分配值而不保存,请使用
assign_attributes
或attributes=
方法。这些方法在 Rails 3 及更高版本中可用。但是,需要注意一些细微的差异和与版本相关的问题。两种方法都遵循以下用法:
请注意,两种方法都不会执行验证或执行回调;调用
save
时将发生回调和验证。Rails 3
attributes=
与 Rails 3 中的assign_attributes
略有不同。attributes=
将检查传递给它的参数是否是哈希,并立即返回如果不是;assign_attributes
没有这样的哈希检查。请参阅 ActiveRecord 属性分配 API 文档 <代码>属性=。以下无效代码将通过简单地返回而不设置属性来默默地失败:
attributes=
将默默地表现得好像分配已成功完成,但实际上并非如此。当
assign_attributes
尝试对封闭数组的哈希键进行字符串化时,此无效代码将引发异常:assign_attributes
将为NoMethodError
引发异常>stringify_keys,表示第一个参数不是哈希。异常本身并不能提供太多关于实际原因的信息,但异常确实发生的事实非常非常重要。这些情况之间的唯一区别是用于批量分配的方法:
attributes=
默默地成功,assign_attributes
引发异常以通知发生了错误。这些例子看起来可能是做作的,而且在某种程度上是做作的,但是当从 API 转换数据时,或者甚至只是使用一系列数据转换而忘记
Hash[]
时,很容易出现这种类型的错误。最终.map
的结果。如果保留上面 50 行的一些代码,并从属性分配中删除 3 个函数,那么您就会遇到失败的情况。Rails 3 的教训是:始终使用
assign_attributes
而不是attributes=
。Rails 4
在 Rails 4 中,
attributes=
只是assign_attributes
的别名。请参阅 ActiveRecord 属性分配 API 文档了解attributes=
。对于 Rails 4,这两种方法都可以互换使用。未能将哈希值作为第一个参数传递将导致非常有用的异常:
ArgumentError:当分配属性时,必须将哈希值作为参数传递。
验证
如果您正在准备中进行飞行前分配对于
保存
,您可能也有兴趣在保存之前进行验证。为此,您可以使用valid?
和invalid?
方法。两者都返回布尔值。如果未保存的模型通过所有验证,valid?
返回 true,否则返回 false。invalid?
只是valid?
的反面valid? 可以这样使用:
这将使您能够处理任何验证问题提前调用
save
。For mass assignment of values to an ActiveRecord model without saving, use either the
assign_attributes
orattributes=
methods. These methods are available in Rails 3 and newer. However, there are minor differences and version-related gotchas to be aware of.Both methods follow this usage:
Note that neither method will perform validations or execute callbacks; callbacks and validation will happen when
save
is called.Rails 3
attributes=
differs slightly fromassign_attributes
in Rails 3.attributes=
will check that the argument passed to it is a Hash, and returns immediately if it is not;assign_attributes
has no such Hash check. See the ActiveRecord Attribute Assignment API documentation forattributes=
.The following invalid code will silently fail by simply returning without setting the attributes:
attributes=
will silently behave as though the assignments were made successfully, when really, they were not.This invalid code will raise an exception when
assign_attributes
tries to stringify the hash keys of the enclosing array:assign_attributes
will raise aNoMethodError
exception forstringify_keys
, indicating that the first argument is not a Hash. The exception itself is not very informative about the actual cause, but the fact that an exception does occur is very important.The only difference between these cases is the method used for mass assignment:
attributes=
silently succeeds, andassign_attributes
raises an exception to inform that an error has occurred.These examples may seem contrived, and they are to a degree, but this type of error can easily occur when converting data from an API, or even just using a series of data transformation and forgetting to
Hash[]
the results of the final.map
. Maintain some code 50 lines above and 3 functions removed from your attribute assignment, and you've got a recipe for failure.The lesson with Rails 3 is this: always use
assign_attributes
instead ofattributes=
.Rails 4
In Rails 4,
attributes=
is simply an alias toassign_attributes
. See the ActiveRecord Attribute Assignment API documentation forattributes=
.With Rails 4, either method may be used interchangeably. Failure to pass a Hash as the first argument will result in a very helpful exception:
ArgumentError: When assigning attributes, you must pass a hash as an argument.
Validations
If you're pre-flighting assignments in preparation to a
save
, you might be interested in validating before save, as well. You can use thevalid?
andinvalid?
methods for this. Both return boolean values.valid?
returns true if the unsaved model passes all validations or false if it does not.invalid?
is simply the inverse ofvalid?
valid?
can be used like this:This will give you the ability to handle any validations issues in advance of calling
save
.