Has Random Token

Has Random Token is a helper to create random tokens for model attributes in Ruby on Rails. The tokens generated are Base64 strings.

Install

gem install 'has_random_token'

or in your Gemfile

gem 'has_random_token' and then bundle

Usage

class Session < ActiveRecord::Base
    has_random_token
end

or

class Session < ActiveRecord::Base
    has_random_token attribute: :code, length: 64, unique: true
end

Configuration

  • The default column name is token. You can customize it by setting the attribute option.
  • By default, it doesn't verify the token is unique. In order to verify uniqueness, you can set the unique option to true.
  • The default token length is 32 characters. You can customize it by setting the length option.