111

I use Rails 3.0.20 and ruby 1.8.7 (2011-06-30 patchlevel 352)

Please suggest me the best plugin to generate guid.

Virtual
  • 2,111
  • 5
  • 17
  • 27
  • http://stackoverflow.com/questions/6021372/best-way-to-create-unique-token-in-rails – Muntasim Aug 14 '13 at 10:03
  • Virtual, there is no way to flag an incorrect answer and the notion of that is questionable. But I strongly suggest you take a look at @apneadiving's answer. Imho, that is the correct answer and a lot of people will miss it because it's at the bottom of the page. – Damon Aw Mar 26 '14 at 15:31
  • 1
    **all new Rails versions provide `Digest::UUID`, e.g. `Digest::UUID.uuid_v4`** – Tilo Jun 26 '22 at 22:58

4 Answers4

314

There are plenty of options, I recommend not to add additional dependencies and use SecureRandom which is builtin:

SecureRandom.uuid #=> "1ca71cd6-08c4-4855-9381-2f41aeffe59c"

See other possible formats here.

Community
  • 1
  • 1
apneadiving
  • 114,565
  • 26
  • 219
  • 213
16

The first thing I would suggest is to please upgrade your ruby and rails versions.

A very good way of generating guids is SecureRandom, which is a ruby module with easy usage.

require 'securerandom'
guid = SecureRandom.hex(10) #or whatever value you want instead of 10
levininja
  • 3,118
  • 5
  • 26
  • 41
QuestionEverything
  • 4,809
  • 7
  • 42
  • 61
3

I would suggest using PostgreSQL and using the uuid column built in, it autogenerates UUID based on type you create the column.

Example in Rails 3 migration

execute <<-SQL
  CREATE TABLE some_items (id uuid PRIMARY KEY DEFAULT uuid_generate_v1());
SQL

Might be a better way to do this in Rails 4.

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
riley
  • 2,387
  • 1
  • 25
  • 31
2

Please see in detail, how to use securerandom ruby standard library to use UUID with example in rails 3.X and 4.X

create usesguid.rb file in your lib/usesguid.rb and paste below code in that -

require 'securerandom'

module ActiveRecord
  module Usesguid #:nodoc:
    def self.append_features(base)
      super
      base.extend(ClassMethods)  
    end

    module ClassMethods
      def usesguid(options = {})
        class_eval do
          self.primary_key = options[:column] if options[:column]
          after_initialize :create_id
          def create_id
            self.id ||= SecureRandom.uuid
          end
        end
      end
    end
  end
end
ActiveRecord::Base.class_eval do
  include ActiveRecord::Usesguid
end

add following line in your config/application.rb to load file -

require File.dirname(__FILE__) + '/../lib/usesguid'

Create migration script for UUID function as mentioned below to -

class CreateUuidFunction < ActiveRecord::Migration
  def self.up
    execute "create or replace function uuid() returns uuid as 'uuid-ossp', 'uuid_generate_v1' volatile strict language C;"
  end

  def self.down
    execute "drop function uuid();"
  end
end

Here is example for contact migration, how we can use it -

class CreateContacts < ActiveRecord::Migration
  def change
    create_table :contacts, id: false do |t|
      t.column :id, :uuid, null:false 
      t.string :name
      t.string :mobile_no

      t.timestamps
    end
  end
end

Final how to use into your model

class Contact < ActiveRecord::Base
  usesguid

end

This will help you to configure UUID for your rails application.

This can be useful for Rails 3.0, 3.1, 3.2 and 4.0 as well.

Please let me know If you have any issue while using it, so simple!

Other options for Rails4 here

Rameshwar Vyevhare
  • 2,699
  • 2
  • 28
  • 34
  • My reason to use this in Rails 4, Actually I wanted to use earlier version of postgreSQL. If you have not such dependancy then can go for builtin feature. – Rameshwar Vyevhare Dec 06 '13 at 10:11