Rails: Update model attribute without invoking callbacks

后端 未结 10 1637
萌比男神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:36

    As a general answer, in Rails 4 this is a simple way to update attributes without triggering callbacks:

    @user.update_column :credits, 5
    

    If you need to update multiple attributes without triggering callbacks:

    @user.update_columns credits: 5, bankrupt: false  
    

    There are other options here in the Rails Guides if you prefer, but I found this way to be the easiest.

提交回复
热议问题