Ruby-on-rails Rails:update_attribute 与 update_attributes
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2778522/
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
Rails: update_attribute vs update_attributes
提问by thenengah
Object.update_attribute(:only_one_field, "Some Value")
Object.update_attributes(:field1 => "value", :field2 => "value2", :field3 => "value3")
Both of these will update an object without having to explicitly tell AR to update.
这两者都将更新对象,而无需明确告诉 AR 进行更新。
Rails API says:
Rails API 说:
for update_attribute
对于 update_attribute
Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default.
更新单个属性并保存记录,而无需通过正常的验证程序。这对于现有记录上的布尔标志特别有用。当验证模块混入时,Base 中的常规 update_attribute 方法被替换为 this ,默认情况下。
for update_attributes
对于 update_attributes
Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned.
从传入的 Hash 更新所有属性并保存记录。如果对象无效,则保存失败并返回false。
So if I don't want to have the object validated I should use update_attribute. What if I have this update on a before_save, will it stackoverflow?
因此,如果我不想验证对象,我应该使用 update_attribute。如果我在 before_save 上有这个更新,它会stackoverflow吗?
My question is does update_attribute also bypass the before save or just the validation.
我的问题是 update_attribute 是否也绕过了 before save 或只是验证。
Also, what is the correct syntax to pass a hash to update_attributes... check out my example at the top.
另外,将哈希传递给 update_attributes 的正确语法是什么...请查看顶部的示例。
回答by Salil
Please refer to update_attribute. On clicking show source you will get following code
请参考update_attribute。单击显示源后,您将获得以下代码
# File vendor/rails/activerecord/lib/active_record/base.rb, line 2614
2614: def update_attribute(name, value)
2615: send(name.to_s + '=', value)
2616: save(false)
2617: end
and now refer update_attributesand look at its code you get
现在参考update_attributes并查看你得到的代码
# File vendor/rails/activerecord/lib/active_record/base.rb, line 2621
2621: def update_attributes(attributes)
2622: self.attributes = attributes
2623: save
2624: end
the difference between two is update_attributeuses save(false)whereas update_attributesuses saveor you can say save(true).
两者之间的区别是update_attribute用途save(false)而update_attributes用途,save或者您可以说save(true)。
Sorry for the long description but what I want to say is important. save(perform_validation = true), if perform_validationis false it bypasses (skips will be the proper word) all the validationsassociated with save.
抱歉,描述太长,但我想说的很重要。save(perform_validation = true)如果perform_validation是假的它绕过(跳过将是正确的字)的所有验证关联save。
For second question
对于第二个问题
Also, what is the correct syntax to pass a hash to update_attributes... check out my example at the top.
另外,将哈希传递给 update_attributes 的正确语法是什么...请查看顶部的示例。
Your example is correct.
你的例子是正确的。
Object.update_attributes(:field1 => "value", :field2 => "value2", :field3 => "value3")
or
或者
Object.update_attributes :field1 => "value", :field2 => "value2", :field3 => "value3"
or if you get all fields data & name in a hash say params[:user]here use just
或者,如果您在哈希中获取所有字段数据和名称,请在params[:user]此处使用
Object.update_attributes(params[:user])
回答by Matt
Tip:update_attributeis being deprecated in Rails 4 via Commit a7f4b0a1. It removes update_attributein favor of update_column.
提示:update_attribute在 Rails 4 中通过Commit a7f4b0a1被弃用。它删除update_attribute支持update_column.
回答by Balachandar1887229
update_attribute
更新属性
This method update single attribute of object without invoking model based validation.
此方法在不调用基于模型的验证的情况下更新对象的单个属性。
obj = Model.find_by_id(params[:id])
obj.update_attribute :language, “java”
update_attributes
更新属性
This method update multiple attribute of single object and also pass model based validation.
此方法更新单个对象的多个属性,并通过基于模型的验证。
attributes = {:name => “BalaChandar”, :age => 23}
obj = Model.find_by_id(params[:id])
obj.update_attributes(attributes)
Hope this answer will clear out when to use what method of active record.
希望这个答案能明确何时使用什么方法的活动记录。
回答by Kibet Yegon
Also worth noting is that with update_attribute, the desired attribute to be updated doesn't need to be white listed with attr_accessibleto update it as opposed to the mass assignment method update_attributeswhich will only update attr_accessiblespecified attributes.
还值得注意的是,与只更新指定属性的批量分配方法相反,update_attribute使用 不需要将要更新的所需属性列入白名单attr_accessible以更新它。update_attributesattr_accessible
回答by Shoaib Malik
update_attributesimply updates only one attribute of a model, but we can pass multiple attributes in update_attributesmethod.
update_attribute只更新模型的一个属性,但我们可以在update_attributes方法中传递多个属性。
Example:
例子:
user = User.last
#update_attribute
user.update_attribute(:status, "active")
It pass the validation
它通过验证
#update_attributes
user.update_attributes(first_name: 'update name', status: "active")
it doesn't update if validation fails.
如果验证失败,它不会更新。
回答by adamliesko
You might be interested in visiting this blog post concerning all the possible ways to assign an attribute or update record (updated to Rails 4) update_attribute, update, update_column, update_columns etc.http://www.davidverhasselt.com/set-attributes-in-activerecord/. For example it differs in aspects such as running validations, touching object's updated_at or triggering callbacks.
您可能有兴趣访问有关分配属性或更新记录的所有可能方法(更新到 Rails 4)update_attribute, update, update_column, update_columns etc.http://www.davidverhasselt.com/set-attributes-in-activerecord/ 的博客文章。例如,它在运行验证、触摸对象的 updated_at 或触发回调等方面有所不同。
As an answer to the OP's question update_attributedoes not by pass callbacks.
作为对 OP 问题的回答update_attribute,不会绕过回调。
回答by Ziv Galili
Great answers. notice that as for ruby 1.9 and above you could (and i think should) use the new hash syntax for update_attributes:
很棒的答案。请注意,对于 ruby 1.9 及更高版本,您可以(我认为应该)对 update_attributes 使用新的哈希语法:
Model.update_attributes(column1: "data", column2: "data")
回答by uma
update_attributeand update_attributesare similar, but
with one big difference: update_attributedoes notrun validations.
update_attribute和update_attributes是相似的,但有一个很大的区别:update_attribute不运行验证。
Also:
还:
update_attributeis used to update record with singleattribute.Model.update_attribute(:column_name, column_value1)update_attributesis used to update record with multipleattributes.Model.update_attributes(:column_name1 => column_value1, :column_name2 => column_value2, ...)
update_attribute用于更新具有单个属性的记录。Model.update_attribute(:column_name, column_value1)update_attributes用于更新具有多个属性的记录。Model.update_attributes(:column_name1 => column_value1, :column_name2 => column_value2, ...)
These two methods are really easy to confuse given their similar names and works. Therefore, update_attributeis being removed in favor of update_column.
鉴于这两种方法的名称和工作方式相似,因此很容易混淆。因此,update_attribute正在被删除以支持update_column.
Now, in Rails4you can use Model.update_column(:column_name, column_value)at the place of Model.update_attribute(:column_name, column_value)
现在,在Rails4 中,您可以Model.update_column(:column_name, column_value)在以下位置使用Model.update_attribute(:column_name, column_value)
Click hereto get more info about update_column.
单击此处获取有关 的更多信息update_column。
回答by rogerdpack
To answer your question, update_attributeskips pre save "validations" but it still runsany other callbacks like after_saveetc. So if you really want to "just update the column and skip any AR cruft" then you need to use (apparently)
要回答您的问题,update_attribute跳过预保存“验证”,但它仍会运行任何其他回调,例如after_save等。因此,如果您真的想“仅更新列并跳过任何 AR cruft”,那么您需要使用(显然)
Model.update_all(...)see https://stackoverflow.com/a/7243777/32453
Model.update_all(...)见https://stackoverflow.com/a/7243777/32453
回答by Wojciech Bednarski
Recently I ran into update_attributevs. update_attributesand validation issue, so similar names, so different behavior, so confusing.
最近我遇到了update_attributevs.update_attributes和验证问题,如此相似的名称,如此不同的行为,如此令人困惑。
In order to pass hash to update_attributeand bypass validation you can do:
为了将哈希传递给update_attribute并绕过验证,您可以执行以下操作:
object = Object.new
object.attributes = {
field1: 'value',
field2: 'value2',
field3: 'value3'
}
object.save!(validate: false)

