020 is definitely the craziest year I have seen in my entire life. What is happening around the world, no one could see that coming. It’s a mad time.

Here are some crazy things that happened:

  • Almost WWIII
  • Australian wildfires
  • COVID-19
  • Trump impeachment
  • Kobe Bryant death
  • George Floyd riots

Meantime, in the Ruby On Rails we are stuck in the old ways. An earlier solution for multitenancy in Rails is still widely used even though it is not optimal in contemporary times.

I am talking about you, https://github.com/influitive/apartment. You were quite a good fellow over the years and truly useful, but it’s time to move on. Let’s start this new decade with something that doesn’t cause so many problems.

Image for post

What is multitenancy?

Multitenancy usually means handling many independent tenants from one app. Most popular in SAAS models. There are several types of implementation. Let’s look at them closer.

Row-level approach

You have a tenant_id column in every DB table and filter by tenant_id in each query.


  • Just another row in the table
  • No brainer
  • Migrations run in O(1) complexity
  • Comes out of the box in Rails
  • No additional costs
  • No operational overhead
  • No cost of switching


  • tenant_id keys everywhere
  • Leaking of data between tenants possible, if you forget about WHERE

Schema-level approach (Apartment gem)

Every tenant has a separate namespace. In practice, we are creating more tables like:

Namespacing is easily achievable with PostgreSQL schemas.


  • No data leakages between tenants
  • Easy extraction of single-tenant data
  • No additional costs


#technology #software-development #ruby-on-rails #architecture #ruby

Ruby on Rails: How to do multitenancy in 2020
22.95 GEEK