Rails: Update model attribute without invoking callbacks

后端 未结 10 1594
萌比男神i
萌比男神i 2021-02-04 23:15

I have a User model that has a :credits attribute. I want a simple button that will add 5 to the user\'s credits, through a route called \"add\" so that /users/3/add would ad

10条回答
  •  迷失自我
    2021-02-04 23:34

    You should be able to use update_all to avoid triggering callbacks.

    def add
     @user = User.find(params[:id])
     User.where(:id=>@user.id).update_all(:credits => @user.credits+5)
     redirect_to root_path
    end
    

    I'd prefer to put this logic in the model, but this should work to solve your original problem as spec'd in the controller.

提交回复
热议问题