Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
Last active July 26, 2016 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixpulvis/b5877ec43e3a7c8e28af5f61dc35fee1 to your computer and use it in GitHub Desktop.
Save nixpulvis/b5877ec43e3a7c8e28af5f61dc35fee1 to your computer and use it in GitHub Desktop.
Retrying a first or create when two happen at the same time.
# 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