ruby
34 lines · 7 steps
How a Rails model builds unique slugs
A Post generates a URL-friendly, collision-free slug before validation and exposes it as the route param.
Explained by
highlit
1class Post < ApplicationRecord
2 before_validation :generate_slug, on: :create
3
4 validates :slug, presence: true, uniqueness: true
5
6 def to_param
7 slug
8 end
9
10 private
11
12 def generate_slug
13 return if title.blank?
14
15 base = normalize(title)
16 candidate = base
17 suffix = 2
18
19 while self.class.where.not(id: id).exists?(slug: candidate)
20 candidate = "#{base}-#{suffix}"
21 suffix += 1
22 end
23
24 self.slug = candidate
25 end
26
27 def normalize(string)
28 I18n.transliterate(string)
29 .downcase
30 .gsub(/[^a-z0-9\s-]/, "")
31 .strip
32 .gsub(/[\s-]+/, "-")
33 end
34end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A before_validation callback lets you populate derived fields before the same record's validations run.
- 2Probing the database for existing values and appending a counter guarantees uniqueness without raising errors.
- 3Overriding to_param swaps the integer id for a human-readable slug in generated URLs.
Related explainers
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
ruby
class SnippetHighlighter CONTEXT_RADIUS = 60 MAX_TERMS = 8
Building search-result snippets in Ruby
regular-expressions
text-processing
search
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-rails-model-builds-unique-slugs-explained-ruby-6473/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.