Use of UUID(Universally unique identifier) in Ruby On Rails Application.

UUID In Ruby On Rails Application :-

I would like to suggest to make use of UUID (Universally Unique Identifier) in the ruby on rails application development. If you want all your objects have unique ID, even across systems and different databases the you can go for UUID (Universally Unique Identifier) .


Ruby On Rails  + UUID

A UUID like the one below is 36 characters long, including dashes .Displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens). For example: 123e4567-e89b-12d3-a456-426655440000

Here are the steps to be followed to develop a sample ror application with uuid. I made use of PostgreSQL database to develop application with UUID.

Rails First Application With UUID Using Postgres As Database :- 

1)Create the application
rails new rails4uuid

2)Create the rvmrc file.
vim .rvmrc
source ~/.rvm/scripts/rvm
type rvm | head -n 1
rvm use ruby-2.1.2
rvm gemset use myreference_rails2.1.2

3)Bundle Install
please add gem ‘pg’ in gem file and bundle install

4)Please verify the database.yml file with proper user name and password.
vim config/database.yml
5)Please install dependies for the UUID
sudo apt-get install libossp-uuid-dev
sudo apt-get install postgresql-contrib-9.1
sudo /etc/init.d/postgresql restart

6)Login to postgres and run following changes.
sudo -u postgres psql postgres
CREATE DATABASE rails4_uuid;
create extension “adminpack”;
create extension “uuid-ossp”;

NOTE:- user is the user name diven in database.yml file in step 4

7) rails g migration enable_uuid_ossp_extension

class EnableUuidOsspExtension < ActiveRecord::Migration
def change
enable_extension ‘uuid-ossp’
rake db:migrate Or

8) To verfiy we can log in to PostgreSQL db and perform this query
SELECT uuid_in(md5(now()::text)::cstring);
9) Create a model for the application , example posts.
rails g model post title:string

10)In migration file please specify id as UUID
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts, id: :uuid do |t|
t.string :title


11) Give rake db:migrate

12)Login to console and create a post to verify uuid is generated.
=> #<Post id: “966d1a3a-7d2e-474f-ab84-4a607326c4ac”, title: “gurudath”, created_at: “2014-10-04 05:15:50”, updated_at: “2014-10-04 05:15:50”>

Not the ruby on rails application with uuid is ready. If any data needed to migrate from one environment to anothre we can directly shared the data from one environment to another without any ID conflicts.

We will update with more information with upcoming days.


Gurudath BN



  1. Article is quite interesting…is even though too technical but to add to above in an enterprise application having validation to have unique master data information be it customer, materials, etc…is a real big challenge … might be there may be some latest tools may suggest some logic but the reality its quite complicated due to dynamic business environment…

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s