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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With