This guide is a sort of extract of official rails guide: Official rails guide
Here I describe an implementation of uuid with PostgreSQL >= 9.4. This requires to enable pgcrypto
Example with a creation of a table named trackers:
class CreateTrackers < ActiveRecord::Migration[5.0]
def change
enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')
create_table :trackers, id: :uuid, default: 'gen_random_uuid()' do |t|
t.integer :duration, null: false
t.string :description, null: false
t.timestamps
end
end
end
You can use uuid to define a relation reference
class CreateProjects < ActiveRecord::Migration[5.0]
def change
enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')
create_table :projects, id: :uuid, default: 'gen_random_uuid()' do |t|
t.string :name
t.uuid :user_id
t.timestamps
end
end
end