Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate unique hashes in Ruby/Rails


I am looking for advise about best practices of generating unique hash strings in Ruby/Rails. Usually I use MD5, SHA etc to do this, but it was not quite straightforward to choose source values for hash (timestamps not always prefered to be used).
So my question are:

  1. What values prefered to be used for generating unique hashes? (database column values, timestamps etc)
  2. Are there any gems for this kind of work?

Any advise would be appreciated.

like image 751
bor1s Avatar asked Oct 25 '11 08:10

bor1s


1 Answers

Use UUIDs:

In ruby 1.9

require 'securerandom'
SecureRandom.uuid

In ruby 1.8

$ gem install uuidtools

UUIDTools::UUID.random_create
like image 157
Gareth Avatar answered Sep 22 '22 06:09

Gareth