Rails:#update_attribute 与 #update_attributes [英] Rails: #update_attribute vs #update_attributes
问题描述
obj.update_attribute(:only_one_field, 'Some Value')obj.update_attributes(field1: 'value', field2: 'value2', field3: 'value3')
这两种方法都将更新一个对象,而无需显式告诉 ActiveRecord 进行更新.
Rails API 说:
<块引用>更新属性
更新单个属性并保存记录,而无需通过正常的验证程序.这对于现有记录上的布尔标志特别有用.混合验证模块时,Base 中的常规 update_attribute 方法将替换为 this,默认情况下.
<块引用>
更新属性
从传入的Hash中更新所有属性并保存记录.如果对象无效,则保存失败并返回false.
因此,如果我不想验证对象,我应该使用 #update_attribute
.如果我在 #before_save
上有这个更新,它会出现 stackoverflow 吗?
我的问题是 #update_attribute
是否也绕过了 before save 或只是验证.
此外,将哈希传递给 #update_attributes
的正确语法是什么...请查看顶部的示例.
请参考 update_attribute
.单击显示源后,您将获得以下代码
# 文件 vendor/rails/activerecord/lib/active_record/base.rb, line 26142614:def update_attribute(名称,值)2615:发送(name.to_s + '=',值)2616:保存(假)2617:结束
现在参考 update_attributes
和看看你得到的代码
# 文件 vendor/rails/activerecord/lib/active_record/base.rb, line 26212621:def update_attributes(属性)2622:self.attributes = 属性2623:保存2624:结束
两者的区别在于update_attribute
使用 save(false)
而 update_attributes
使用 save
或者你可以说save(true)
.
抱歉描述太长,但我想说的很重要.save(perform_validation = true)
,如果perform_validation
是假的,它绕过(skips 将是正确的词)所有 验证 与 save
相关联.
第二个问题
<块引用>此外,将哈希传递给 update_attributes 的正确语法是什么...请查看顶部的示例.
你的例子是正确的.
Object.update_attributes(:field1 => "value", :field2 => "value2", :field3 => "value3")
或
Object.update_attributes :field1 =>"值", :field2 =>"value2", :field3 =>值3"
或者如果您获得所有字段数据 &散列中的名称说 params[:user]
在这里只使用
Object.update_attributes(params[:user])
obj.update_attribute(:only_one_field, 'Some Value')
obj.update_attributes(field1: 'value', field2: 'value2', field3: 'value3')
Both of these will update an object without having to explicitly tell ActiveRecord to update.
Rails API says:
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.
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.
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?
My question is does #update_attribute
also bypass the before save or just the validation.
Also, what is the correct syntax to pass a hash to #update_attributes
... check out my example at the top.
Please refer to update_attribute
. On clicking show source you will get following code
# 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_attributes
and look at its code you get
# 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_attribute
uses save(false)
whereas update_attributes
uses save
or you can say save(true)
.
Sorry for the long description but what I want to say is important. save(perform_validation = true)
, if perform_validation
is false it bypasses (skips will be the proper word) all the validations associated with save
.
For second question
Also, what is the correct syntax to pass a hash to update_attributes... check out my example at the top.
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
Object.update_attributes(params[:user])
这篇关于Rails:#update_attribute 与 #update_attributes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!