Created
July 21, 2017 01:13
-
-
Save cainlevy/574041ce4517737ca50aa554a4d1f4f0 to your computer and use it in GitHub Desktop.
catch_uniqueness_errors
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
# 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