Created
March 17, 2012 14:44
-
-
Save corny/2060282 to your computer and use it in GitHub Desktop.
A handler for PostgreSQL Foreign Key Constraint Exceptions to insert in your ApplicationController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
# A foreign key constraint exception from the database | |
rescue_from PG::Error do |exception| | |
message = exception.message | |
if message.include?('foreign key constraint') | |
logger.warn(message) | |
# shorten the message | |
message = message.match(/DETAIL: .+/).to_s | |
redirect_to :back, | |
:flash => {:error => "Whatever you tried to do - the server is unable to process your request because of a foreign key constraint. (#{message})" } | |
else | |
# anything else | |
raise exception | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment