Ruby-on-rails Rails 回形针:更新与 update_attributes
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27684038/
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 Paperclip: update vs. update_attributes
提问by Derrick Mar
I realized something quite strange when attempting to upload an image via the paperclip gem for my user model (under the avatar attribute). For some reason there User.update and @user.update_attributes behaves differently. Does anyone know why this is so?
尝试通过回形针 gem 为我的用户模型(在 avatar 属性下)上传图像时,我意识到了一些很奇怪的事情。出于某种原因, User.update 和 @user.update_attributes 的行为不同。有谁知道为什么会这样?
#using @user.update_attributes(user_avatar_params)
def update_profile_pic
@user = User.find(params[:id])
@user.update_attributes(user_avatar_params)
puts @user.avatar_file_name.nil? # prints false as expected
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
#using User.update(@user.id, user_avatar_params)
def update_profile_pic
@user = User.find(params[:id])
User.update(@user.id, user_avatar_params)
puts @user.avatar_file_name.nil? # prints true although successfully saves
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
And here is my strong params in the user_controller.rb
这是我在 user_controller.rb 中的强参数
def user_avatar_params
params.require(:user).permit(:avatar)
end
UPDATE
更新
So after receiving prompt and awesome responses this is what I learned:
因此,在收到及时而精彩的回复后,这就是我学到的:
User.updatereturns the resulting object whether it was saved successfully to the database or not. Therefore, User.update(@user.id, user_avatar_params)does not update the @user variable if you do not make the assignment. @user.update_attributes(user_avatar_params)does change the @user variable implicitly.
User.update无论结果对象是否成功保存到数据库,都返回结果对象。因此,User.update(@user.id, user_avatar_params)如果您不进行分配,则不会更新 @user 变量。@user.update_attributes(user_avatar_params)确实隐式地改变了@user 变量。
The solution for this to work with the update method is to do
@user = User.update(@user.id, user_avatar_params)
与更新方法一起使用的解决方案是
@user = User.update(@user.id, user_avatar_params)
采纳答案by aaron-coding
ActiveRecord.updatehas a behavior that may be throwing you off:
ActiveRecord.update有一种可能会让你失望的行为:
Updates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not. http://apidock.com/rails/ActiveRecord/Base/update/class
如果验证通过,则更新一个对象(或多个对象)并将其保存到数据库中。无论对象是否成功保存到数据库,都会返回结果对象。 http://apidock.com/rails/ActiveRecord/Base/update/class
However the update_attributeswill just return false.
然而,update_attributes只会返回false。
Both of these use Model-level validations and so both should save or not save equally. However, the return values will be different.
这两者都使用模型级别的验证,因此两者都应该平等地保存或不保存。但是,返回值会有所不同。
As @RoaringStones pointed out, the solution is to use
正如@RoaringStones 指出的,解决方案是使用
user = User.update(user.id, user_avatar_params)
回答by jamesconant
For what it's worth, as of Rails 4.0.2, #updatereturns falseif the update failed, not simply the object which the update failed for. Of further note, #update_attributesis simply an alias of #updatenow.
对于它的价值,从 Rails 4.0.2 开始,如果更新失败则#update返回false,而不仅仅是更新失败的对象。进一步注意,#update_attributes只是#updatenow的别名。
回答by kenju
By the way, #update_attributesgonna be deprecated from Rails 6 (though this is not released yet)
顺便说一句,#update_attributesRails 6 将被弃用(尽管尚未发布)
please have a look at
请看看
- https://github.com/rails/rails/pull/31998
- https://github.com/rails/rails/commit/5645149d3a27054450bd1130ff5715504638a5f5
- https://github.com/rails/rails/pull/31998
- https://github.com/rails/rails/commit/5645149d3a27054450bd1130ff5715504638a5f5
for more details.
更多细节。

