Search code examples
rubye-commercechecksum

Best way to generate order numbers for an online store?


Every order in my online store has a user-facing order number. I'm wondering the best way to generate them. Criteria include:

  • Short
  • Easy to say over the phone (e.g., "m" and "n" are ambiguous)
  • Unique
  • Checksum (overkill? Useful?)
  • Edit: Doesn't reveal how many total orders there have been (a customer might find it unnerving to make your 3rd order)

Right now I'm using the following method (no checksum):

def generate_number                
    possible_values = 'abfhijlqrstuxy'.upcase.split('') | '123456789'.split('')

    record = true
    while record
        random = Array.new(5){possible_values[rand(possible_values.size)]}.join
        record = Order.find(:first, :conditions => ["number = ?", random])
    end          
    self.number = random
end

Solution

  • As a customer I would be happy with:

    year-month-day/short_uid
    

    for example:

    2009-07-27/KT1E
    

    It gives room for about 33^4 ~ 1mln orders a day.