Simple has_token-Plugin for Rails 3 (and newer) and Active Record.
Ruby HTML JavaScript CSS
Switch branches/tags
Latest commit 337d562 Jul 14, 2016 @neuling neuling committed on GitHub update rails version


A token generator for Rails 3 and Active Record (also works with Rails 4).


Put the following gem requirement in your Gemfile:

gem "da-has_token"


Make sure your model has a field called token (type string).

class Entry < ActiveRecord::Base  
  # Use default options (size 10, chars a-z + A-Z + 0-9)
  # Use custom token size
  # has_token size: 25
  # Use custom set of characters

The token is set in a before_create callback. There is a possible race condition when checking if a newly generated token already exists – make sure to add an unique index to the token column in the database.

A NoMoreTokens error is raised if a new unique token can not be generated within HasToken::TokenGenerator::MAX_TRIES.

There are similar plugins providing more features configuration options, e.g.

This project is released under the MIT license.