mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 13:06:56 +08:00
Correctly handle exception
This commit is contained in:
@ -12,11 +12,10 @@ class ActiveRecord::Base
|
|||||||
# OR
|
# OR
|
||||||
#
|
#
|
||||||
# find_by(hash) || create_or_find_by(hash) (if we are generally finding)
|
# find_by(hash) || create_or_find_by(hash) (if we are generally finding)
|
||||||
#
|
|
||||||
def self.find_or_create_by_safe!(hash)
|
def self.find_or_create_by_safe!(hash)
|
||||||
begin
|
begin
|
||||||
find_or_create_by!(hash)
|
find_or_create_by!(hash)
|
||||||
rescue PG::UniqueViolation
|
rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique
|
||||||
# try again cause another transaction could have passed by now
|
# try again cause another transaction could have passed by now
|
||||||
find_or_create_by!(hash)
|
find_or_create_by!(hash)
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user