ruby
41 lines · 8 steps
How a multitenant concern scopes Rails models
A concern that automatically confines every query, insert, and validation to the current tenant's account.
Explained by
highlit
1module Multitenant
2 extend ActiveSupport::Concern
3
4 included do
5 belongs_to :account, inverse_of: false
6
7 default_scope do
8 if Current.account
9 where(account_id: Current.account.id)
10 else
11 raise Multitenant::MissingTenantError, "#{name} queried without a current account"
12 end
13 end
14
15 before_validation :assign_current_account, on: :create
16
17 validate :account_matches_current_tenant
18 end
19
20 class_methods do
21 def unscoped_across_tenants
22 unscope(where: :account_id)
23 end
24 end
25
26 private
27
28 def assign_current_account
29 self.account ||= Current.account
30 end
31
32 def account_matches_current_tenant
33 return if account_id.blank? || Current.account.nil?
34
35 unless account_id == Current.account.id
36 errors.add(:account, "does not match the current tenant")
37 end
38 end
39end
40
41class Multitenant::MissingTenantError < StandardError; end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A default scope tied to a request-scoped Current attribute keeps every model query confined to one tenant without touching call sites.
- 2Failing loudly when no tenant is set prevents accidental cross-tenant data leaks better than silently returning everything.
- 3Assigning and validating the tenant on save closes the write side of the isolation the default scope enforces on reads.
Related explainers
ruby
class Admin::AccountsController < Admin::BaseController before_action :set_account, only: :destroy def destroy
A guarded destroy action in Rails
controllers
callbacks
transactions
Intermediate
8 steps
ruby
class MailingListDeduplicator GMAIL_DOMAINS = %w[gmail.com googlemail.com].freeze def initialize(subscribers)
Deduplicating a mailing list by canonical email
deduplication
normalization
service object
Intermediate
8 steps
ruby
class ProgressBar BAR_WIDTH = 40 def initialize(total, io: $stdout)
Building a terminal progress bar in Ruby
state tracking
terminal output
time estimation
Intermediate
8 steps
ruby
class Product < ApplicationRecord scope :search, ->(query) { return all if query.blank?
Building a multi-term search scope in Rails
arel
scopes
full-text search
Advanced
7 steps
ruby
module Rack class MaintenanceMode RETRY_AFTER = 3600
A Rack maintenance-mode middleware in Rails
middleware
rack
http-status
Intermediate
8 steps
ruby
class ImageNormalizer ORIENTATION_TRANSFORMS = { 1 => ->(img) {}, 2 => ->(img) { img.flop },
Correcting EXIF orientation in Ruby
lookup-table
lambdas
image-processing
Intermediate
7 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-multitenant-concern-scopes-rails-models-explained-ruby-44f6/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.