diff options
author | Sean McGivern <sean@gitlab.com> | 2019-02-05 17:41:00 +0300 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2019-02-05 17:41:00 +0300 |
commit | d6be539bf1be90d81ac4ad7aac94aedebac10c43 (patch) | |
tree | 96993829bc0d4437ddf6363afa3aed710a824ea5 /app/models | |
parent | 5dc15b2a3b5fee22bb905c1e178e3ea6300d02c3 (diff) | |
parent | ccd8a9b2821c964b85533c253430041712ef195e (diff) |
Merge branch 'bvl-codeowner-rules-model-ce' into 'master'
Adds helper for `find_or_create_by` in transaction
See merge request gitlab-org/gitlab-ce!24913
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/application_record.rb | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 29696ab276f..c4e310e638d 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -6,4 +6,12 @@ class ApplicationRecord < ActiveRecord::Base def self.id_in(ids) where(id: ids) end + + def self.safe_find_or_create_by(*args) + transaction(requires_new: true) do + find_or_create_by(*args) + end + rescue ActiveRecord::RecordNotUnique + retry + end end |