Last active
July 26, 2016 16:42
-
-
Save nixpulvis/b5877ec43e3a7c8e28af5f61dc35fee1 to your computer and use it in GitHub Desktop.
Retrying a first or create when two happen at the same time.
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
# Simple helper function for retrying a first or create when two happen | |
# at the same time and one violates the database unique index constraint. | |
def first_or_create(scope, attributes = {}) | |
begin | |
scope.first_or_create(attributes) | |
rescue ActiveRecord::RecordNotUnique | |
retry | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment