Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please see in detail, how to use securerandom ruby standard library to use UUID with example in rails 3.X and 4.X</p> <p>create usesguid.rb file in your lib/usesguid.rb and paste below code in that -</p> <pre><code>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 </code></pre> <p>add following line in your config/application.rb to load file -</p> <pre><code>require File.dirname(__FILE__) + '/../lib/usesguid' </code></pre> <p>Create migration script for UUID function as mentioned below to -</p> <pre><code>class CreateUuidFunction &lt; 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 </code></pre> <p>Here is example for contact migration, how we can use it -</p> <pre><code>class CreateContacts &lt; 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 </code></pre> <p>Final how to use into your model </p> <pre><code>class Contact &lt; ActiveRecord::Base usesguid end </code></pre> <p>This will help you to configure UUID for your rails application. </p> <p>This can be useful for Rails 3.0, 3.1, 3.2 and 4.0 as well.</p> <p>Please let me know If you have any issue while using it, so simple!</p> <p>Other options for Rails4 <a href="http://blog.lingohub.com/developers/2013/10/dev-bit-auto-generate-uuid-postgres-rails-4/" rel="nofollow">here</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload