Skip to content

Instantly share code, notes, and snippets.

@corny
Created March 17, 2012 14:44
Show Gist options
  • Save corny/2060282 to your computer and use it in GitHub Desktop.
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
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