mongoid_uniquify

Generate unique token in a Mongoid field. Mongoid version of Ryan Bates uniquify gem.

Installation

gem install mongoid_uniquify

In your Gemfile

gem "mongoid_uniquify"

REQUIREMENTS:

  • mongoid 2.0.0 or later

USAGE:

class Project
  include Mongoid::Document
  include Mongoid::Uniquify

  uniquify :token, :length => 7, :chars => ('a'..'z').to_a + ('0'..'9').to_a

  field :token, :type => String

  # ...
end

If you want to use generated token as key and add unique index, do something like below.

class Project
  include Mongoid::Document
  include Mongoid::Uniquify

  uniquify :token, :length => 7, :chars => ('a'..'z').to_a + ('0'..'9').to_a

  field :token, :type => String
  key :token

  index :token, :unique => true

  # ...
end

Copyright © 2011 Kichiro IKEMOTO. See LICENSE.txt for further details.