MongoMapper + ActiveRecord Unique Value Generator
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Unique Generator Build Status Dependency Status


Unique Generator is a plugin for ActiveRecord and MongoMapper which makes it simple to generate unique fields and unique tokens e.g. for invites, api keys and the like.

Unique Generator is only available for Rails 3.0+.


Installing Unique Generator is simple, just add the following to your Gemfile:

gem 'unique_generator', '~> 1.0'

We'll automatically hook into ActiveRecord and MongoMapper via a railtie, adding three methods.


Unique Generator defines a very simple method, adding only four methods:

  • YourModelClass.generate_random(length = 32) - Generates a case sensitive field with size characters.
  • YourModelClass.generate_unique(length = 32, &block) - Taking a block which defines the predicate of a given value, it will generate a random toke of the specified length until the block returns true.
  • YourModelClass#unique_field?(name) - Called on an instance, tells you whether that instances value for a given field is unique in the database.

And finally, for most people, the only method that matters:

  • YourModelClass#generate_unique_field!(name, length = 32) - Generates a random token of the specified length, combining the above methods to ensure it has a unique value.

Note: Please keep in mind, in your migration your field accepting a random token should ideally be case sensitive.


As a simple example, say we want to generate a unique invite token on a model, to do this, we'd do something like:

class Invite < ActiveRecord::Base

  before_save :generate_invite_token

  def generate_invite_token
    generate_unique_field! :token, 32 if token.blank?




We encourage all community contributions. Keeping this in mind, please follow these general guidelines when contributing:

  • Fork the project
  • Create a topic branch for what you’re working on (git checkout -b awesome_feature)
  • Commit away, push that up (git push your_remote awesome_feature)
  • Create a new GitHub Issue with the commit, asking for review. Alternatively, send a pull request with details of what you added.
  • Once it’s accepted, if you want access to the core repository feel free to ask! Otherwise, you can continue to hack away in your own fork.

Other than that, our guidelines very closely match the GemCutter guidelines here.

(Thanks to GemCutter for the contribution guide)


RocketPants is released under the MIT License (see the license file) and is copyright Filter Squad, 2012.