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.