Skip to content

Instantly share code, notes, and snippets.

@n3tr
Forked from nicalpi/1.Gemfile
Created February 15, 2012 04:22
Show Gist options
  • Save n3tr/1833169 to your computer and use it in GitHub Desktop.
Save n3tr/1833169 to your computer and use it in GitHub Desktop.
Creates easy shorten UUID in your RAILS APP
## See http://
gem 'base32-crockford', :require => 'base32/crockford'
rails g migration AddPermanentUuidToDocuments permanent_uuid:string
rake db:migrate
class Document < ActiveRecord::Base
[..]
before_validation :set_permanent_uuid, :on => :create
validates_presence_of :permanent_uuid
validates_uniqueness_of :permanent_uuid
[..]
def to_param
"#{permanent_uuid}"
end
protected
def set_permanent_id
# If you want to a shorter UUID you can play with other options than Time
self.permanent_uuid = Base32::Crockford.encode(Time.now.to_i)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment