No description, website, or topics provided.
Ruby HTML JavaScript CSS
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


This gem provides a has_ helper to populate a field with an obscured version of a model id.

Why Obscure an ID?


  1. Add to your Gemfile:

     gem 'obscured_id'
  2. Install the gem:

     bundle install
  3. Generate the default initializer:

     rails generate obscured_id:initializer
  4. Generate and run a migation for the model you wish to have an obscured id:

     rails generate obscured_id:migration MODEL_NAME
     rake db:migrate
  5. Add the obscured_id declaration to your model:

  6. Populate your database with the obscured id:

     rake obscured_id:populate[MODEL_NAME]


The following options can be configured with a Rails initializer:

  • obscured_id_secret_token: This is the secret token used when generating the obscured id. It is strongly recommended you run the obscured_id:initializer generator to set this, otherwise the default is nil.
  • obscured_id_field: This is the field in which you wish to store the obscured id. Default is "obscured_id"
  • record_obscured_ids: If set to false, the obscured_id will not automatically be set. Default is true.




This creates an initializer with a random secret key for obscuring the model's id. While it is possible to run this plugin without a secret key, it's not recommended.

obscured_id:migration MODEL_NAME (--field=FIELD_NAME)

This creates a migration for a model with the name. The field option lets you override the default field name "obscured_id". If you override the field name, be sure to set the field option in your configuration or has_obscured_id declaration.

Rake Tasks

rake obscured_id:populate[MODEL_NAME]

This will populate a model with an obscured id without recording timestamps. NOTE: that this will populate your model's obscured ID field regardless of your record_obscured_ids configuration or auto option in the has_obscured_id declaration.

Has Declaration

When added to your model definition, has_obscured_id declaration configures your model to be obscure id capable, adding the methods described below.

The following options are available:

  • field: This overides obscured_id_field.
  • secret_token: This overides obscured_id_secret_token.
  • auto: If set to false, the obscured_id will not automatically be set.


class ThingWithObscuredId < ActiveRecord::Base

class ThingWithConfiguredObscuredId < ActiveRecord::Base
  has_obscured_id :field => :hidden_id, :secret_token => "ee71c598567e9d469738b7ec64213b8c", :auto => false

Instance Methods

  • set_obscured_id: Force sets the instance's obscured ID.
  • safe_set_obscured_id: Sets an instance's obscured ID only if updating is turned on.

Class Methods

  • set_obscured_ids: Force populates obscured ids for all records. Returns an array of integers, where the first is the count of succesful updates, and the second is the count of failed updates.


You can turn updating off:

    ActiveRecord::Base.record_obscured_ids = false

And turn it back on:

    ActiveRecord::Base.record_obscured_ids = true

Running Tests

$ bundle install
$ rspec spec


Copyright (c) 2013 Larry Halff, released under the MIT license