Reputation: 12675
I have a transaction in one of my model. When something go wrong, I want to be notified.
If it would be transaction in controller, I would simply do:
begin
(my transaction...)
rescue => exception
ExceptionNotifier.deliver_exception_notification(exception, self, request, data)
end
But I want to do similar thing in my model, passing nils
as self
and request
does not help. What can I do about it?
Upvotes: 0
Views: 335
Reputation: 1185
In our project, we use it differently:
For model, we create an initializer in project_directory/config/initializers to add it to ActiveRecord::Base.
class ActiveRecord::Base
include ExceptionNotifiable
end
Doing this, all models exception will invoke ExceptionNotifier email as per our configuration.
For controllers, we include it in our ApplicationController so if there is an exception in any controllers, we will receive an email.
class ApplicationController < ActionController::Base
include ExceptionNotifiable
.....
end
For transaction in the controller, i will do:
class MyController < ApplicationController
.....
def create
MyModel.transaction do
.....
end
end
end
Without rescuing the exception, the ExceptionNotifier will be called automatically.
Hopefully it helps.
Upvotes: 1