Rails: Update model attribute without invoking callbacks

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

    To update multiple attributes without callbacks you can use update_all in your model as so:

    self.class.update_all({name: value, name: value}, self.class.primary_key => id)
    

    If you really want you can even try even a update_columns method and mixin this to your active record base class.

    To update one attribute you can use update_column. In addition there is some specific methods that can found in the rails guides http://guides.rubyonrails.org/active_record_callbacks.html#skipping-callbacks

提交回复
热议问题