Back to guides

Ruby on Rails

Multi-Tenant SaaS in Rails 8: Row-Level Scoping, Schemas & Isolation Risks

DEVRUBY engineering team 8 min read

Multi-tenancy looks simple in a Rails prototype until a background job emails the wrong customer or a cache key serves another company's report. In Rails 8, teams usually choose between three database isolation models: shared tables with a tenant_id column, PostgreSQL schemas per tenant, or a separate database per tenant. The right choice depends less on preference and more on tenant count, compliance requirements, and how strictly you enforce tenant context outside HTTP requests.

Planning a Rails or Ruby upgrade? Book a free 30-minute second opinion on your upgrade path, risky gems, or agency quote.

Three isolation models and when each fits

Every multi-tenant Rails architecture trades operational simplicity against isolation strength. Picking the wrong one early either slows down every migration or creates a painful rewrite when enterprise customers ask for data isolation.

  • Row-level scoping (shared database, tenant_id column): the default fit for most B2B SaaS apps with hundreds or thousands of tenants. Migrations run once, connection pools stay small, and analytics across tenants are straightforward — provided every query is scoped.
  • Schema-per-tenant (PostgreSQL schemas): keeps tenant tables separated inside one database, but migrations must run across every schema and connection pooling becomes harder once you pass a few hundred active tenants.
  • Database-per-tenant (Rails multi-DB or SQLite per tenant): strongest isolation for regulated industries or enterprise plans with custom retention, at the cost of per-tenant provisioning, backups, and rolling schema migrations.

Enforcing tenant context safely in Rails 8

If you use row-level scoping, never rely on developers remembering .where(account: current_account) on every controller action. Set the active tenant once at the boundary (subdomain, path prefix, or authenticated session) using ActiveSupport::CurrentAttributes, and make unscoped queries fail loudly in development and test.

Avoid broad default_scope traps that silently break joins or admin reports. Instead, pair explicit tenant associations with database-level composite foreign keys (account_id, record_id) or PostgreSQL Row-Level Security (RLS) so even a raw SQL query cannot cross tenant boundaries.

Where tenant leaks actually happen

Most real-world multi-tenant bugs do not happen inside standard controller requests — they happen where HTTP middleware never runs:

  • ActiveJob and Solid Queue: background jobs execute outside the request lifecycle. Serialize tenant_id explicitly in job arguments (or middleware) and restore Current.account before querying anything.
  • Rails.cache and Solid Cache: include the tenant identifier in every cache key. A shared key like 'dashboard/stats' leaks metrics across accounts immediately.
  • Active Storage and file exports: authorize every blob download against the current tenant and avoid predictable public URLs for private PDFs or CSV exports.
  • ActionCable and Turbo Streams: scope every broadcast channel name by tenant ID and verify tenant membership inside Connection#connect and Channel#subscribed.

Testing isolation before enterprise audits

Add a dedicated isolation test suite that seeds two tenants in every run and asserts that Tenant A cannot read, update, export, or trigger background jobs on Tenant B's IDs. When enterprise buyers send security questionnaires, automated tenant-isolation tests and PostgreSQL RLS policies are concrete proof rather than promises.

Key takeaways

  • Start with row-level scoping plus strict tenant boundaries unless compliance requires schema or database-per-tenant isolation.
  • Back up application-level scoping with composite keys or PostgreSQL Row-Level Security so missed scopes cannot leak data.
  • Audit non-HTTP entry points first: ActiveJob/Solid Queue, cache keys, ActionCable streams, and Active Storage downloads.
  • Maintain a two-tenant automated test suite that runs on every pull request.

Related

Want to review your case with context?

Share the codebase, workflow, or systems involved and the result you need. We will use the call to decide a practical next step.

More guides

Ruby on Rails

Rails 7 to 8 Upgrade Guide: What Breaks and How to Plan the Upgrade

How to upgrade a production app from Rails 7 to Rails 8: the version path, the Ruby requirement, the changes that actually break code, and a safe rollout plan.

AI workflow automation

AI Automation for Small Businesses: What to Automate First and What to Leave to People

How small businesses can choose their first AI automation: where language models remove real work, where simple rules work better, and how to stay in control.