Skip to content

Instantly share code, notes, and snippets.

@nicalpi
Created July 22, 2011 14:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nicalpi/1099608 to your computer and use it in GitHub Desktop.
Save nicalpi/1099608 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