Tom Cantwell

In application.rb, add the following line:

config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
end

Create a new migration from the command line:

rails g migration enable_uuid

You should get a file that looks like this:

class EnableUuid < ActiveRecord::Migration[6.0]end

Then add this method inside that migration to enable uuid:

class EnableUuid < ActiveRecord::Migration[6.0]
def change
enable_extension 'uuid-ossp'
enable_extension 'pgcrypto'
end
end

Finally, in your regular migrations, you can set the id type to uuid:

class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users, id: :uuid do |t|
t.string :username
t.timestamps
end
end
end

When you run rails db:migrate, your schema for theUser model should then look like this:

create_table "devices", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "username"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

--

--