Correctly handle exception

This commit is contained in:
Sam 2018-08-23 16:26:42 +10:00
parent bd66fbef7f
commit 6ee352510a
1 changed files with 1 additions and 2 deletions

View File

@ -12,11 +12,10 @@ class ActiveRecord::Base
# OR
#
# find_by(hash) || create_or_find_by(hash) (if we are generally finding)
#
def self.find_or_create_by_safe!(hash)
begin
find_or_create_by!(hash)
rescue PG::UniqueViolation
rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique
# try again cause another transaction could have passed by now
find_or_create_by!(hash)
end