Global Exception handler


Is there a way in Ruby or Rails to have a global exception handler to rescue from a StaleObject exception? I know about rescue_action_in_public, but I don’t know how to get the object the exception occurred on.

For example, while updating a record identified as @my_record, a StaleObject exception is thrown. I would like to implement a global way of grabbing the class variable in the exception handling and automatically comparing all the attributes from params[:my_record] to @my_record.

If the record has become stale on attributes that weren’t changed by the user (i.e. user 1 changed field “title”, while user 2 only changed field “summary”), although the record has become stale, updating the record with the updated fields on the newer version of the record doesn’t need additional user intervention. If the attributes changed by both users are identical, it would be handled differently.

I know I’m simplifying things a bit, but I think you pretty much get the picture.

I just want to avoid having to repeat myself all the time:


rescue ActiveRecord::StaleObjectError

if automatically_handlestaleobject(@my_record)

redirect_to …


render “a_layout_or_even_rjs_to_let_the_user_handle_it”



Best regards

Peter De Berdt