RubyConf 2025 in Atlanta brought together Ruby engineers from across the ecosystem to discuss the state of UUID adoption following RFC 9562’s publication in May 2024. Sessions focused on ActiveRecord’s UUID primary key support, the performance implications of UUID v7 versus v4 for Rails applications on PostgreSQL, and plans for native UUID v7 support in the Ruby standard library. See UUID in Ruby on Rails for the full implementation guide.

Key topics covered

Rails 8 UUID primary keys — Rails 8’s improved UUID support was a central theme. The recommended pattern for PostgreSQL 18:

# db/migrate/xxxx_create_orders.rb
class CreateOrders < ActiveRecord::Migration[8.0]
  def change
    enable_extension 'pg_uuidv7' unless extension_enabled?('pg_uuidv7')

    create_table :orders, id: false, force: :cascade do |t|
      t.column :id, :uuid, primary_key: true, default: -> { "uuidv7()" }
      t.string  :status,    null: false
      t.uuid    :customer_id, null: false
      t.timestamps
    end
  end
end
# app/models/order.rb
class Order < ApplicationRecord
  self.primary_key = 'id'

  # No before_create hook needed — DB generates UUID v7 automatically
  belongs_to :customer
  has_many   :order_items
end

Sessions noted that delegating UUID v7 generation to PostgreSQL (via uuidv7()) rather than Rails ensures all INSERT paths — including raw SQL migrations and database seeds — get v7 values. See PostgreSQL 18 Ships Native UUID v7.

App-layer generation with SecureRandom

For Rails apps on PostgreSQL 14–17 (without native uuidv7()):

# config/initializers/uuid_v7.rb
require 'securerandom'

module UUIDv7
  def self.generate
    ts = (Time.now.to_f * 1000).to_i
    hex = ts.to_s(16).rjust(12, '0')
    rand_a = SecureRandom.hex(2)
    rand_b = SecureRandom.hex(8)
    # version nibble = 7, variant bits = 10xxxxxx
    ver_rand_a = (rand_a.to_i(16) & 0x0FFF | 0x7000).to_s(16).rjust(4, '0')
    var_rand_b = (rand_b[0..1].to_i(16) & 0x3F | 0x80).to_s(16).rjust(2, '0')
    "#{hex[0..7]}-#{hex[8..11]}-#{ver_rand_a}-#{var_rand_b}#{rand_b[2..]}"
  end
end
# app/models/concerns/uuid_v7_primary_key.rb
module UuidV7PrimaryKey
  extend ActiveSupport::Concern
  included do
    before_create { self.id ||= UUIDv7.generate }
  end
end

For production use, the pg_uuidv7 extension for PostgreSQL 14–17 is more robust. See Zero-Downtime UUID Migration for switching an existing Rails app.

B-tree performance gains on PostgreSQL

Sessions presented benchmarks comparing UUID v4 and v7 insert throughput on a PostgreSQL 16 database with 100M rows. UUID v7 showed 40–60% better INSERT performance due to reduced B-tree page splits — consistent with the analysis in UUID v4 vs v7. The improvement was more pronounced at scale: at 10M rows the difference was ~15%, widening to ~55% at 500M rows.

Ruby 3.4 UUID v7 proposal

RubyConf 2025 included a proposal to add SecureRandom.uuid_v7 to Ruby 3.4, mirroring Python 3.13’s addition of uuid.uuid7(). The proposal was well-received with a tracking issue opened on the Ruby bug tracker. See Python 3.13 adds uuid.uuid7() for the Python precedent.

Further reading

External references

Frequently asked questions

How is UUID v7 used in Rails ActiveRecord after RFC 9562?

Rails 8 ships with has_secure_uuid for token generation and supports UUID primary keys via id: :uuid in migrations. For UUID v7, override the default uuid_generate_v4() PostgreSQL function with uuidv7() from PostgreSQL 18 or the pg_uuidv7 extension. In the migration: enable_extension 'pg_uuidv7' then create_table :orders, id: false do |t| t.column :id, :uuid, primary_key: true, default: -> { "uuidv7()" } end. See UUID in Ruby on Rails.

Is there a native UUID v7 method in Ruby?

Ruby's standard library does not yet include a UUID v7 method as of Ruby 3.3. The uuidtools gem provides UUIDTools::UUID.timestamp_create (UUID v1), but not v7. The cleanest approach for v7 is delegating to PostgreSQL 18's uuidv7() function via default: -> { "uuidv7()" } in ActiveRecord migrations. Sessions at RubyConf 2025 discussed adding native uuid7 support to Ruby 3.4. See UUID in Ruby on Rails.