Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cch1/uuid_primary_key

Rails Plugin that gently allows you to use UUIDs as the primary key.
https://github.com/cch1/uuid_primary_key

Last synced: 25 days ago
JSON representation

Rails Plugin that gently allows you to use UUIDs as the primary key.

Awesome Lists containing this project

README

        

UUIDPrimaryKey
==============

Copyright 2006-2007, Chris Hapgood
MIT License
Derived from the works of several others, including Demetrio Nunes, Paul Dix and Lee Jensen.

Requirements: UUIDTools GEM (gem install uuidtools)

In any model class requiring a UUID PK, invoke UUIDPrimaryKey, optionally with
the name of the PK column in your database. Example:

class Person < ActiveRecord::Base
UUIDPrimaryKey
end

class Place < ActiveRecord::Base
UUIDPrimaryKey :column => 'uuid'
end

Hints:
1. To override the value of the PK from the application, define an
initialize method in your model like this:
class Person < ActiveRecord::Base
UUIDPrimaryKey :column => 'uuid'

def initialize(params = nil)
super
self.id = params[:uuid] unless params[:uuid].nil?
end
end

2. To define a reasonable colum using migrations, try this:
class AddPeople < ActiveRecord::Migration
def self.up
create_table :people, :id => false do |t|
t.column :uuid, :string, :limit => 36
t.column :firstnames, :string, :limit => 55
t.column :lastname, :string, :limit => 35
t.column :created_at, :timestamp
t.column :updated_at, :timestamp
end
execute("ALTER TABLE people ADD PRIMARY KEY(uuid)")
end

def self.down
drop_table :people
end
end