how to find the cause of ActiveRecord ROLLBACK

后端 未结 4 1856
小鲜肉
小鲜肉 2020-12-07 22:40

In the logs I\'m seeing a ROLLBACK, but no exception is logged. Is there a way to find out what caused the ROLLBACK?

Here\'s the excerpt of the log:

4条回答
  •  执笔经年
    2020-12-07 23:03

    1) Disable before_create, before_save, before_update and check where it saves the day

    2) If rollback was caused by one of those methods, check that those methods return true when you don't plan to rollback.

    For example if you set default value for boolean field to avoid nil, you would probably do it this way

    def set_defaults_before_create
      self.my_boolean_field ||= false
    end
    

    In this example method set_defaults_before_create always returns false and thus rollbacks your transaction. So refactor it to return true

    def set_defaults_before_create
      self.my_boolean_field ||= false
      true
    end
    

提交回复
热议问题