Rails 3: Generate unique codes (coupons)

后端 未结 6 1177
借酒劲吻你
借酒劲吻你 2021-01-30 18:29

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

Thanks.

6条回答
  •  盖世英雄少女心
    2021-01-30 19:16

    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

    md5(Time.now.to_i.to_s+Time.now.usec.to_s)
    

提交回复
热议问题