Rails Paperclip: update vs. update_attributes

心已入冬 提交于 2019-12-10 00:38:17

问题


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?

#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

def user_avatar_params
  params.require(:user).permit(:avatar)
end

UPDATE

So after receiving prompt and awesome responses this is what I learned:

User.update returns 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.

The solution for this to work with the update method is to do @user = User.update(@user.id, user_avatar_params)


回答1:


ActiveRecord.update has a behavior that may be throwing you off:

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

However the update_attributes will just return 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

user = User.update(user.id, user_avatar_params)



回答2:


For what it's worth, as of Rails 4.0.2, #update returns false if the update failed, not simply the object which the update failed for. Of further note, #update_attributes is simply an alias of #update now.




回答3:


By the way, #update_attributes gonna be deprecated from Rails 6 (though this is not released yet)

please have a look at

  • https://github.com/rails/rails/pull/31998
  • https://github.com/rails/rails/commit/5645149d3a27054450bd1130ff5715504638a5f5

for more details.



来源:https://stackoverflow.com/questions/27684038/rails-paperclip-update-vs-update-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!