diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2019-02-04 14:39:54 +0100 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2019-02-05 14:44:41 +0100 |
commit | ccd8a9b2821c964b85533c253430041712ef195e (patch) | |
tree | fb39012eb785dcfaa6b46a062e32b3fe4a042d53 /app/models/application_record.rb | |
parent | d6b39ea7fb31e243c59b0ca66b0fd4de3296f004 (diff) | |
download | gitlab-ce-ccd8a9b2821c964b85533c253430041712ef195e.tar.gz |
Adds helper for `find_or_create_by` in transaction
This allows us to call `find_or_create_by` on all models and scopes.
Diffstat (limited to 'app/models/application_record.rb')
-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 |