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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With