Prevent infinite loop when updating attributes within after_commit, :on => :create

后端 未结 3 1030
太阳男子
太阳男子 2020-12-20 15:02

I create an infinite callback loop when I need to update an attribute during an after_commit, :on => :create. It only occurs if I need to update an attribute

3条回答
  •  礼貌的吻别
    2020-12-20 15:45

    The problem will be update_attributes will initiate the after_commit callback you've used

    As mentioned by Rafael, you'd either need to use a callback not triggered by update_attributes, or use an update method which negates callbacks:


    update_column does not initiate callbacks - self.update_column(filename: filename)


    after_create only fires when you create a record (not update) - after_create :generate

提交回复
热议问题