Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: Generate unique codes (coupons)

What's the best way to generate unique codes to use as coupon codes?

Thanks.

like image 553
donald Avatar asked Dec 29 '10 22:12

donald


3 Answers

In Ruby's standard library there is SecureRandom for this:

SecureRandom.hex(3)

The length of the output is double of what the length input specified.

like image 169
Ryan Bigg Avatar answered Nov 01 '22 20:11

Ryan Bigg


What you want is to generate a GUID. See here:

guid generator in ruby

like image 27
zsalzbank Avatar answered Nov 01 '22 18:11

zsalzbank


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

gem install uuid
cd /myproject/path
uuid-setup

In your code

require_gem 'uuid'
my_unique_id_var = UUID.new

Reference: http://railsforum.com/viewtopic.php?id=12616#p44545

like image 4
dombesz Avatar answered Nov 01 '22 18:11

dombesz