UUID in Ruby
Ruby UUID Generator
Ruby's answer is one method from the standard library: SecureRandom.uuid, no gem involved.
Standard librarySecureRandom.uuiduuid_v7
Generated locally with the Web Crypto API — nothing leaves your browser.
Generating UUIDs in Ruby
SecureRandom.uuidreturns a version 4 UUID as a lowercase hyphenated string, sourced from the platform CSPRNG. It is part ofsecurerandom, which ships with Ruby — so a Rails app needs no dependency to mint an identifier.- Newer releases of
securerandomalso provideSecureRandom.uuid_v7, which makes a time-ordered v7 value; that is the one to use as a primary key on a high-insert table, because sequential keys keep inserts at the end of the index. For storage, pair it with the right column type — PostgreSQLuuid, MySQL/ActiveRecordbinary(16)— rather than a 36-charactervarchar.
How to generate
- Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
- Copy one value with Copy, or set How many and use Copy all for a fixture list.
- Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.
Use Cases
- ActiveRecord primary keys — generate the ID in Ruby so the record knows it before
save. - Public-facing tokens — reset links and API keys that must not be guessable from the next one.
- Idempotency keys for Sidekiq jobs that can be retried.
How Ruby UUID Generator Compares
| SecureRandom.uuid | v4, standard library, CSPRNG |
| SecureRandom.uuid_v7 | v7, time-ordered — newer securerandom |
| SecureRandom.hex(16) | 32 random hex characters — not a UUID, no version bits |
Code Examples
Ruby
require "securerandom"
SecureRandom.uuid # v4
# => "b8e2f1c0-3a7d-4e11-9c2d-1f0a6b7c8d9e"
SecureRandom.uuid_v7 # v7, time-ordered
# A batch for fixtures
Array.new(5) { SecureRandom.uuid }ActiveRecord migration
create_table :orders, id: :uuid, default: "gen_random_uuid()" do |t|
t.string :reference
t.timestamps
end
# or, generating in Ruby:
class Order < ApplicationRecord
before_create { self.id ||= SecureRandom.uuid }
endFrequently Asked Questions
Do I need a gem to generate UUIDs in Ruby?
No —
SecureRandom.uuid is in the standard library. A gem is only worth it if you also want parsing, validation or the other versions.How do I get a UUID v7 in Ruby?
SecureRandom.uuid_v7, available in newer releases of the securerandom gem. On an older Ruby, use the uuid7 gem or generate it in PostgreSQL.What column type should store a UUID in Rails?
On PostgreSQL,
id: :uuid (16 bytes). On MySQL, binary(16). A varchar(36) works but wastes more than twice the space per row.Related tools
UUID v1 GeneratorUUID v3 GeneratorUUID v4 GeneratorUUID v5 GeneratorUUID v6 GeneratorUUID v7 GeneratorUUID v8 GeneratorBulk UUID GeneratorShort UUID GeneratorUUID ValidatorUUID FormatterJavaScript UUID GeneratorTypeScript UUID GeneratorNode.js UUID GeneratorReact UUID GeneratorPython UUID GeneratorJava UUID GeneratorC# UUID GeneratorGo UUID GeneratorPHP UUID GeneratorRuby UUID GeneratorRust UUID GeneratorKotlin UUID GeneratorSwift UUID GeneratorSQL UUID Generator