---
title: "The Better Primary Key: A Guide to ULIDs for Rails Developers"
url: https://daily.dev/posts/the-better-primary-key-a-guide-to-ulids-for-rails-developers-j3arvwvba
source_url: https://norvilis.com/the-better-primary-key-a-guide-to-ulids-for-rails-developers
type: article
source: "Ruby Flow"
published: 2026-05-28T00:19:07.690Z
updated: 2026-05-28T01:40:00.294Z
tags: ["postgresql", "rails"]
reading_time: 4
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# The Better Primary Key: A Guide to ULIDs for Rails Developers

**[Ruby Flow](https://daily.dev/sources/rubyflow)** · 4 min read · 0 upvotes · 0 comments

## Summary

ULIDs (Universally Unique Lexicographically Sortable Identifiers) offer a compelling alternative to UUIDs as primary keys in Rails apps. Unlike random UUIDs that fragment B-Tree indexes, ULIDs embed a timestamp prefix making them chronologically sortable while remaining globally unique. A 4-step implementation guide covers installing the `ulid` gem, creating a reusable ActiveRecord Concern to auto-generate IDs on record creation, modifying migrations to use string primary keys, and including the concern in models. Key advantages over UUIDs include better write performance (sequential index appends), improved readability via Crockford's Base32 alphabet, and zero infrastructure setup compared to Snowflake IDs.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://norvilis.com/the-better-primary-key-a-guide-to-ulids-for-rails-developers>

## Similar posts on daily.dev

- [UUID v7 vs. ULID: Which Sortable ID Should You Choose?](https://daily.dev/posts/uuid-v7-vs-ulid-which-sortable-id-should-you-choose--xlgz26hfk) · Ruby Flow · 0 upvotes · 0 comments
- [Universally Unique Lexicographically Sortable Identifiers \(ULIDs\)](https://daily.dev/posts/universally-unique-lexicographically-sortable-identifiers-ulids--xfthi2f2b) · Marius Bancila · 0 upvotes · 0 comments

---

Tags: [#postgresql](https://daily.dev/tags/postgresql), [#rails](https://daily.dev/tags/rails)

[View this post on daily.dev](https://daily.dev/posts/the-better-primary-key-a-guide-to-ulids-for-rails-developers-j3arvwvba)
