Skip to content

Instantly share code, notes, and snippets.

@oojikoo-gist
Created January 7, 2016 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oojikoo-gist/fd88ad616cade9ff8678 to your computer and use it in GitHub Desktop.
Save oojikoo-gist/fd88ad616cade9ff8678 to your computer and use it in GitHub Desktop.
rails: uuid enabling

enabling UUID on rails

$ rails g migration enable_uuid_extension

psql

postgres=# CREATE EXTENSION "uuid-ossp";

CREATE EXTENSION
postgres=# SELECT uuid_generate_v4();
           uuid_generate_v4
--------------------------------------
 f8c9ffd6-a234-4729-bd2a-68379df315fb
(1 row)

Migration to enable uuid

class EnableUuidExtension < ActiveRecord::Migration
  def change
  	enable_extension 'uuid-ossp'
  end
end

create model with uuid default

create_table :users, id: :uuid do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

add uuid ot model

class AddUuidToBooks < ActiveRecord::Migration
  def change
    add_column :books, :uuid, :uuid, default: 'uuid_generate_v4()'
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment