| name | custom-id |
| description | This skill should be used when the user asks to "add custom_id", "install custom_id", "use custom_id", "add Stripe-style prefixed IDs", "generate usr_abc123 style IDs", "use a string primary key with a prefix", "add the cid macro", "embed parent ID chars into child ID", "use the related option in cid", "set up a database trigger to generate IDs", or when working with the custom_id gem in a Rails application. |
| version | 1.0.0 |
custom_id Skill
custom_id adds a cid class macro to ActiveRecord models that generates
prefixed, Base58, Stripe-style string IDs on before_create — no UUID, no
sequential integer leakage.
What It Does
user = User.create!
user.id
class User < ApplicationRecord
cid "usr"
end
user = User.create!
user.id
ID format: "#{prefix}_#{random}" where random is size Base58 characters.
Base58 alphabet omits visually ambiguous characters (0, O, I, l):
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
Installation
See references/installation.md for full steps. Quick summary:
gem "custom_id"
bundle install
rails custom_id:install
The initializer auto-includes CustomId::Concern into every ActiveRecord::Base
subclass. No manual include needed in individual models.
cid Macro Signature
cid(prefix, size: 16, related: {}, name: :id)
| Parameter | Default | Purpose |
|---|
prefix | required | String prepended before _ (e.g. "usr") |
size | 16 | Length of random Base58 portion (not total ID length) |
name | :id | Column to populate — use :id or any other string column |
related | {} | Embed parent ID chars: { association_name => chars_to_borrow } |
Total ID length = prefix.length + 1 + size
Core Usage
Basic — default primary key
class User < ApplicationRecord
cid "usr"
end
create_table :users, id: :string do |t|
t.string :name, null: false
t.timestamps
end
user = User.create!(name: "Alice")
user.id
Custom size
class ApiKey < ApplicationRecord
cid "key", size: 32
end
Non-primary-key column
class Article < ApplicationRecord
cid "art", name: :slug, size: 12
end
article = Article.create!(title: "Hello")
article.id
article.slug
Pre-setting the attribute skips generation:
Article.create!(title: "Custom", slug: "art_my_custom_code")
Related — embed parent ID chars
class Workspace < ApplicationRecord
cid "wsp"
end
class Document < ApplicationRecord
belongs_to :workspace
cid "doc", size: 22, related: { workspace: 6 }
end
workspace = Workspace.create!
workspace.id
doc = Document.create!(workspace: workspace)
doc.id
Constraint: size must be strictly greater than chars_to_borrow.
cid "doc", size: 6, related: { workspace: 6 } raises ArgumentError.
⚠️ Critical Rules
Migration must use id: :string
create_table :users, id: :string do |t|; end
create_table :users, id: :string, default: nil do |t|; end
cid after belongs_to when using related:
class Document < ApplicationRecord
belongs_to :workspace
cid "doc", size: 22, related: { workspace: 6 }
end
class Document < ApplicationRecord
cid "doc", size: 22, related: { workspace: 6 }
belongs_to :workspace
end
MySQL + DB triggers require cid on the model too
MySQL's LAST_INSERT_ID() returns 0 for non-AUTO_INCREMENT columns.
Always declare cid on the model alongside a MySQL trigger — the gem
generates the ID in Ruby before INSERT so ActiveRecord reads the correct value.
Common Mistakes
class Order < ApplicationRecord
cid "ord"
cid "ord"
end
class Order < ApplicationRecord
cid "ord"
end
cid "doc", related: { workspace_id: 6 }
cid "doc", related: { workspace: 6 }
Multiple cid Calls (Different Columns)
class Contract < ApplicationRecord
cid "ctr"
cid "ref", name: :ref_number, size: 8
end
Additional Resources
Reference Files
references/installation.md — Full setup steps, initializer content, troubleshooting
references/patterns.md — All usage patterns with copy-paste examples
references/db-triggers.md — Database trigger alternative for PostgreSQL, MySQL, SQLite
Examples
examples/basic.rb — Basic models, custom size, non-PK column
examples/related.rb — Parent-child ID embedding with related:
examples/db_triggers.rb — Migration + trigger setup per adapter
examples/testing.rb — Minitest patterns for models using cid