ruby
36 lines · 8 steps
Enforcing scoped uniqueness in Rails
A Membership model normalizes email and guarantees one member per organization, backed by a matching database index.
Explained by
highlit
1class Membership < ApplicationRecord
2 belongs_to :organization
3 belongs_to :user
4
5 before_validation :normalize_email
6
7 validates :email, presence: true
8 validates :email,
9 uniqueness: {
10 scope: :organization_id,
11 case_sensitive: false,
12 message: "is already a member of this organization"
13 }
14
15 validates :role, inclusion: { in: %w[owner admin member guest] }
16
17 scope :for_organization, ->(org) { where(organization: org) }
18
19 private
20
21 def normalize_email
22 self.email = email.to_s.strip.downcase.presence
23 end
24end
25
26class AddMembershipsUniqueIndex < ActiveRecord::Migration[7.1]
27 disable_ddl_transaction!
28
29 def change
30 add_index :memberships,
31 "organization_id, lower(email)",
32 unique: true,
33 name: "index_memberships_on_org_and_lower_email",
34 algorithm: :concurrently
35 end
36end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Normalizing input before validation makes case-insensitive uniqueness reliable and predictable.
- 2Model-level uniqueness validations should always be backed by a matching database unique index to close race conditions.
- 3A functional index on lower(email) lets the database enforce the same case-insensitive rule the model expresses.
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/enforcing-scoped-uniqueness-in-rails-explained-ruby-9288/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.