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) .
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;
ALTER USER user WITH SUPERUSER;
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’
end
end
rake db:migrate Or EnableUuidOsspExtension.new.change
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
t.timestamps
end
end
end
11) Give rake db:migrate
12)Login to console and create a post to verify uuid is generated.
Post.create(title:’gurudath’)
=> #<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.
Regards
Gurudath BN
Nice Post
LikeLiked by 1 person
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…
LikeLiked by 1 person
Good post, knowledgeable
LikeLiked by 1 person
Super
LikeLike
Nice post guru
LikeLike
The article is very useful for I T people. I wish you good luck for furthur articles. Good luck
yours loving father
nagaraj
LikeLike
Dude an awesome article need to learn from you alott
LikeLike
Very nice post. I simply stumbled upon your blog and wished to mention that I’ve truly enjoyed browsing your blog posts.
In any case I’ll be subscribing in your rss feed and I am
hoping you write once more very soon!
LikeLike