Skip to content

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

License

Notifications You must be signed in to change notification settings

cch1/uuid_primary_key

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages