문제

One of the things that is driving me crazy with Rails is that I'll see a ROLLBACK message in the console with no reason attached to the rollback. This often leads me into hunting for some validation error, but it would nice to have a more detailed message.

Is there anyway to enabled more detailed logging for db rollbacks?

도움이 되었습니까?

해결책

You could use a after_rollback callback.

Create a module called RollbackLogger and place it inside your app/concerns directory

module RollbackLogger
  extend ActiveSupport::Concern

  included do
    after_rollback :log_status, on: [:create, :update]
  end

  def log_status
    Rails.logger.info "Rollback caused by: #{self.errors.full_messages}"
  end
end

then include this module in every ActiveRecord model:

class Foo < ActiveRecord::Base
  include RollbackLogger
end

Edit:

As Mr. Damien Roche suggests, you can create a new file inside the config/initializers directory and add the following line:

ActiveRecord::Base.send(:include, RollbackLogger)

All models will include the RollbackLogger module automatically.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top