本文介绍了Rails:update_attribute vs update_attributes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Object.update_attribute(:only_one_field,Some Value)
Object.update_attributes(:field1 =>value,:field2 =>value2 :field3 =>value3)

这两个都会更新对象, AR更新。



Rails API说:



for update_attribute

for update_attributes

/ p>

所以如果我不想让对象验证,我应该使用update_attribute。如果我有一个before_save这个更新,它会stackoverflow吗?



我的问题是update_attribute也绕过保存或只是验证。



另外,将散列传递给update_attributes的正确语法是...查看顶部的示例。

解决方案

请参阅。点击显示源代码,您将获得以下代码

 #文件供应商/ rails / activerecord / lib / active_record / base.rb 2614 
2614:def update_attribute(name,value)
2615:send(name.to_s +'=',value)
2616:save(false)
2617:end

现在引用,并查看其获取的代码

 #文件供应商/ rails / activerecord / lib / active_record / base.rb,第2621行
2621:def update_attributes(attributes)
2622:self.attributes = attributes
2623:save
2624:end

两者之间的区别是使用,而使用,或者您可以说。



对不起,很长的描述,但我想说的是重要的。 ,如果 perform_validation 为false,它跳过(跳过将正确的单词)所有与保存合作。



对于第二个问题

您的示例是正确的。

  Object.update_attributes(:field1 =>value,:field2 =>value2,:field3 =>value3)

  Object.update_attributes:field1 => value,:field2 => value2,:field3 => value3

名称在散列中表示 params [:user] 此处仅使用

  Object.update_attributes(params [:user])


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.

Rails API says:

for update_attribute

for update_attributes

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 assosciated with save.

For second question

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 vs update_attributes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:48