Assumption : Rails v6+, Postgres v9.1+

Ideally you will implement this configuration before you've created any tables.

Create a migration :  rails g migration postgres_uuid

class postgres_uuid < ActiveRecord::Migration[6.0]
    def change
        enable_extension 'pgcrypto'
    end
end

Migrate :  rails db:migrate

Configuration done.

Models and tables

Once you have UUIDs enabled remember to ensure you specify UUIDs when creating tables.

Example migration using UUIDs.

class CreateInvoices < ActiveRecord::Migration[6.0]

  def change
  
    create_table :invoices, id: :uuid do |t|

    	t.string :type
	t.string :name
		
	t.timestamps

    end
  end
end

Generators

If you use generators you can update your application config to ensure that generators run in the future use UUID for their primary keys.

Create a new file in your project:  config/initializers/generators.rb

Rails.application.config.generators do |gs|
  gs.orm :active_record, primary_key_type: :uuid
end

Default Sort

One side efffect of using UUIDs as our primary keys is that the default sort order will be "broken" - in that .first and .last will be ordered on the random nature of UUID's.

To resolve this situation we need to notify ActiveRecord that the default sort order should be created_at.

Update /app/models/application_record.rb

class ApplicationRecord < ActiveRecord::Base

  self.abstract_class = true
  
  # Adjust default sort order
  self.implicit_order_column = :created_at
  
end

End.