Recently I wrote coupon-code gem that does exactly the same thing. The algorithm borrowed from Algorithm::CouponCode CPAN module.
A coupon code should not only be unique, but also easy to read and type while it still is secure. Neil's explanation and solution is great. This gem provides a convenient way to do it and a bonus validation feature.
Maybe try this, seems to be more proof than just generating some random key.
They say: UUID generator for producing universally unique identifiers based on RFC 4122 (http://www.ietf.org/rfc/rfc4122.txt).
http://rubygems.org/gems/uuid
If you don't want to waste comparing against the database (not a super expensive operation), you can guarantee that Time is always unique because it only occurs once
What you want is to generate a GUID. See here:
guid generator in ruby
You can do something like this too:
But then you would have to compare against a database to make sure it is not used yet.
From here.
In Ruby's standard library there is
SecureRandom
for this:The length of the output is double of what the length input specified.
Note: There is a same question
Recently I wrote coupon-code gem that does exactly the same thing. The algorithm borrowed from Algorithm::CouponCode CPAN module.
A coupon code should not only be unique, but also easy to read and type while it still is secure. Neil's explanation and solution is great. This gem provides a convenient way to do it and a bonus validation feature.
Maybe try this, seems to be more proof than just generating some random key. They say: UUID generator for producing universally unique identifiers based on RFC 4122 (http://www.ietf.org/rfc/rfc4122.txt). http://rubygems.org/gems/uuid
In your code
Reference: http://railsforum.com/viewtopic.php?id=12616#p44545
If you don't want to waste comparing against the database (not a super expensive operation), you can guarantee that Time is always unique because it only occurs once