Skip to content

Instantly share code, notes, and snippets.

@cainlevy
Created July 21, 2017 01:13
Show Gist options
  • Save cainlevy/574041ce4517737ca50aa554a4d1f4f0 to your computer and use it in GitHub Desktop.
Save cainlevy/574041ce4517737ca50aa554a4d1f4f0 to your computer and use it in GitHub Desktop.
catch_uniqueness_errors
# forgiveness is faster than permission
private def catch_uniqueness_errors
yield
rescue ActiveRecord::RecordNotUnique => e
field = e.cause.message.match(/index_([a-z_]*)_on_([a-z_]*)/)[2]
raise unless field
errors.add(field, :taken)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment