Skip to content

Latest commit

 

History

History
16 lines (15 loc) · 462 Bytes

retry_n_times.md

File metadata and controls

16 lines (15 loc) · 462 Bytes

Retry n times

Example found here

def generate_unique_secure_token(attribute)
  10.times do |i|
    SecureRandom.hex(12).tap do |token|
      if exists?(attribute => token)
        raise "Couldn't generate a unique token in 10 attempts!" if i == 9
      else
        return token
      end
    end
  end
end