Rails gem · 2026 · Author and maintainer
tenant_kit
Row-level multi-tenancy for Rails, strict by default. One database, a tenant key on every owned table, and no cross-tenant leaks, not even in background jobs.
Overview
tenant_kit is a Ruby gem published on RubyGems under the MIT licence. It makes a Rails app multi-tenant with the shared-schema strategy: a tenant_id on owned tables, automatic query scoping through ActiveSupport::CurrentAttributes, auto-assignment of the current tenant on create, and first-class propagation of the tenant into Active Job. It is strict by default: querying a tenant-scoped model with no tenant set raises instead of returning another tenant's data.
Key Features
- belongs_to_tenant scopes every read, assigns the tenant on create and validates its presence
- Strict mode: a query with no current tenant raises NoTenantSet rather than leaking rows
- The current tenant rides into Active Job through GlobalID and is restored around perform, so it survives Solid Queue
- One-line request resolution by subdomain, domain, header or a custom filter
- validates_uniqueness_to_tenant folds the tenant into uniqueness checks
- TenantKit.without_tenant is the loud, greppable escape hatch
- Zero runtime dependencies beyond Rails; Ruby 3.3+ and Rails 7.2+ with Rails 8 as the primary target
The Challenge
Most Rails SaaS apps end up multi-tenant, and the row-level approach is the easiest to operate. But the usual gems leave the dangerous part to you: forget to set a tenant and a query quietly returns everyone's data. Background jobs are the classic leak, because the request's tenant is gone by the time the job runs.
The Approach
I built the scoping on ActiveSupport::CurrentAttributes rather than a raw thread-local, made the default behaviour raise when no tenant is set, and serialised the tenant into every job with GlobalID so it is restored before perform. Controller helpers resolve the tenant from the request in one line, and the install generator scaffolds the tenant model and initializer.
The Outcome
Published to RubyGems in July 2026 with CI on GitHub Actions and a full test suite. It powers the multi-tenant workspaces in RailsAI Kit and the client portals in the school platforms.