Using retry to rescue ActiveRecord::RecordNotUnique
If you are using database unique indexes (and you should!) you may end up having to deal with ActiveRecord::RecordNotUnique because of database racing conditions.
To rescue from such exceptions you can use the Ruby retry method:
def self.set_flag( user_id, flag )
# Making sure we only retry 2 times
tries ||= 2
flag = UserResourceFlag.where( :user_id => user_id , :flag => flag).first_or_create!
rescue ActiveRecord::RecordNotUnique
retry unless (tries -= 1).zero?
end
Written by Gabriel Cebrian
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#