ActiveRecordPrettyKey
A Ruby gem for generating and managing pretty, human-readable keys in ActiveRecord models.
The gem automatically generates pretty keys by creating unique integer "tickets" and encoding them with the Sqids library to produce short, URL-safe string identifiers. Keys are generated before saving records, ensuring both uniqueness and consistency.
Inspried by:
Why Pretty Keys?
| Feature | Regular Primary Keys | UUID7 | Pretty Keys |
|---|---|---|---|
| Human Readable | ✅ Sequential numbers (1, 2, 3...) | ❌ Long, random strings | ✅ Short, memorable strings |
| In Order | ✅ Yes | ✅ Yes | ❌ No |
| URL Friendly | ✅ Simple and clean | ❌ Long and unwieldy | ✅ Short and clean |
| Security | ❌ Predictable, easily guessable | ✅ Random and secure | ✅ Obfuscated but readable |
| Length | ✅ Very short | ❌ Very long (36 chars) | ✅ Short (8-12 chars) |
| Collision Risk | ✅ None (auto-increment) | ✅ Extremely low | ✅ None (ticket database table) |
Pretty keys offer a middle ground between auto-incrementing integers and UUIDs — they're human-readable identifiers that remain secure, performant, and user-friendly.
Installation
Add this line to your application's Gemfile:
gem 'active_record_pretty_key'And then execute:
$ bundle installOr install it yourself as:
$ gem install active_record_pretty_keyUsage
Setup
First, run the generator to create the required tickets table:
rails generate active_record_pretty_key:installThis will create a migration that sets up the tickets table needed for generating unique IDs.
Customizing or Randomising the Sqids Alphabet
Configration of a customized Sqids ID alphabet can be done by setting Rails credentials like so:
sqids:
alphabet: "ENwUpVBslGq25afc6i0hyD4tnjxRY31Wouz7HFTeMKgSbmPZvJ9ALXCQdrkI8O"This can be used to configure a randomized alphabet to obfuscate object IDs.
To generate a shuffled alphabet:
require 'sqids'
puts Sqids::DEFAULT_ALPHABET.split('').shuffle.joinCreating Models
Pretty keys require that your model use a string primary key:
class CreatePosts < ActiveRecord::Migration[8.0]
def change
create_table :posts, id: :string do |t|
t.string :title
t.timestamps
end
end
endYou may choose to configure ActiveRecord to always generate migrations with a string primary key:
# application.rb
# Default string primary key in migrations for use with Sqids
config.generators do |generate|
generate.orm :active_record, primary_key_type: :string
endIncluding the Concern
Include the concern in your ApplicationRecord to use everywhere:
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
include ActiveRecordPrettyKey::Concern
endOR
Include the concern on a model by model basis:
class Post < ApplicationRecord
include ActiveRecordPrettyKey::Concern
endGenerating Keys
Keys are generated before-save automatically:
post = Post.create(title: "My Awesome Post")
post.id
# => "BnJe"Or the generation helper can be called manually:
post = Post.new(title: "My Awesome Post")
post.generate_pretty_key
# => "9LW9"
post.save!Active Storage
When working with Active Storage remember to properly set the attachments foreign key type:
class FixAttachmentsFks < ActiveRecord::Migration[8.0]
def change
change_column :active_storage_attachments, :record_id, :string, null: false
end
endDevelopment
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/noreastergroup/active_record_pretty_key. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the ActiveRecordPrettyKey project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.