Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Ruby
  3. Apartment

Apartment

Ruby

A Ruby gem for implementing database multi-tenancy in Rails applications using PostgreSQL schemas or separate databases.

GitHubGitHub
2.7k stars458 forks0 contributors

What is Apartment?

Apartment is a Ruby gem that provides database multi-tenancy for Rails applications, enabling data to be partitioned by tenant (e.g., account or company) while allowing some data to remain in a common shared tenant. It solves the problem of isolating tenant data in SaaS applications, ensuring data separation and security. The gem supports PostgreSQL schemas or separate databases for tenant isolation.

Target Audience

Rails developers building multi-tenant SaaS applications, such as platforms serving multiple companies or accounts with isolated data. It's also suitable for teams needing to scale data partitioning across different databases or servers.

Value Proposition

Developers choose Apartment for its seamless integration with Rails, support for PostgreSQL schemas (which improves performance and Heroku compatibility), and flexible tenant-switching mechanisms via Rack middleware. Its active maintenance and extensibility (e.g., custom elevators, parallel migrations) make it a robust choice over simpler multi-tenancy solutions.

Overview

Database multi-tenancy for Rack (and Rails) applications

Use Cases

Best For

  • Building SaaS platforms with isolated data per customer
  • Implementing multi-tenancy in Rails apps using PostgreSQL schemas
  • Scaling tenant data across multiple databases or servers
  • Switching tenants dynamically based on subdomain or domain in web requests
  • Managing parallel migrations for large numbers of tenants
  • Excluding specific models (like global users) from tenant isolation

Not Ideal For

  • Applications using MySQL or SQLite without PostgreSQL schema emulation
  • Teams needing simple, drop-in multi-tenancy without custom Rack middleware or elevator configuration
  • Projects on older ActiveRecord versions (like 4.x) where memory issues are a critical concern
  • Non-Rails frameworks or applications not using ActiveRecord for database interactions

Pros & Cons

Pros

PostgreSQL Schema Optimization

Leverages PostgreSQL schemas for tenant isolation, offering better performance and seamless compatibility with platforms like Heroku, as highlighted in the README for scalable deployments.

Flexible Tenant Switching

Provides multiple built-in elevators (e.g., subdomain, domain) and custom options via Rack middleware, enabling dynamic tenant routing per request without manual code changes.

Dynamic Tenant Management

Includes tools for programmatically creating, dropping, and migrating tenants, along with rake tasks for automation, simplifying lifecycle management in multi-tenant apps.

Excluded Models Support

Allows specific models like User or Company to remain in the global namespace, ensuring shared data accessibility across all tenants while maintaining isolation for others.

Cons

Complex Middleware Setup

Requires careful Rack middleware ordering to avoid issues with gems like Devise or better_errors, adding configuration overhead and potential debugging headaches.

Memory Issues with Older ActiveRecord

Admits to excessive memory growth on ActiveRecord 4.x due to PostgreSQL data type mapping bugs, forcing upgrades or patches that complicate maintenance.

PostgreSQL-Centric Design

Optimized for PostgreSQL schemas; other databases (e.g., MySQL) lack equivalent features, limiting flexibility and requiring workarounds for non-PostgreSQL setups.

Frequently Asked Questions

Quick Stats

Stars2,688
Forks458
Contributors0
Open Issues116
Last commit2 years ago
CreatedSince 2012

Tags

#rails#rack-middleware#ruby-gem#activerecord#multi-tenancy#ruby-on-rails#saas

Built With

R
Rails
R
Ruby
P
PostgreSQL
R
Rack
A
ActiveRecord

Included in

Ruby14.1k
Auto-fetched 1 day ago

Related Projects

PaperTrailPaperTrail

Track changes to your rails models

Stars7,021
Forks910
Last commit1 month ago
ActsAsTaggableOnActsAsTaggableOn

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.

Stars4,996
Forks1,193
Last commit3 months ago
AncestryAncestry

Organise ActiveRecord model into a tree structure

Stars3,873
Forks472
Last commit1 month ago
AuditedAudited

Audited (formerly acts_as_audited) is an ORM extension that logs all changes to your Rails models.

Stars3,495
Forks662
Last commit6 months ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub