Last Updated: February 25, 2016
·
4.765K
· gabceb

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