Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby generate unique integer for use as a primary key

I need to generate a unique integer that will be assigned to the id field within a rails app. What is the best way of doing this. (using the usual auto increment is not an option and it has to be an integer.)

like image 820
chris morgan Avatar asked Aug 31 '11 09:08

chris morgan


1 Answers

Ruby 1.9 has UUID version 4 generation included in module SecureRandom:

> require 'securerandom'
 => true
> SecureRandom.uuid
 => "4b3a56db-4906-4a44-a262-975d80c88195" 
> SecureRandom.uuid.gsub("-", "").hex
 => 56667719780883163491780810954791777167

A bit lengthy, but unique for sure.

like image 134
Kimmo Lehto Avatar answered Oct 27 '22 15:10

Kimmo Lehto