Fixing Apartment's ActiveRecord::ConnectionNotEstablished error

I’ve run into this problem when using the Apartment gem for Ruby on Rails 5.2. If is taking forever to load and you’re seeing verbose console logs starting with ActiveRecord::ConnectionNotEstablished (No connection pool with ‘primary’ found), there’s a threading issue happening with your server. This is how to fix it:

  1. Inside puma.rb, find the line:

    threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }

    And change it to:

    threads_count = 1
  2. Then restart your server. If you’re running Apartment and want to use subdomains with, you’ll want to restart your server with the correct parameters:

    rails s -p 3000 -b

I haven’t encountered this error in production (where I’m on Heroku and using PostgreSQL as my database), so it might pertain to development environments only. However, it’s still annoying if you’re trying to develop an application.

If you’re here from my step-by-step Apartment tutorial:

How to set up Apartment for multi-tenant Rails apps

About the author

I'm Mark Thomas Miller, a full stack engineer and designer currently working at ConvertKit. (We're hiring!) People like Arnold Schwarzenegger, Lindsey Stirling, and Tim Ferriss use features I've built to connect with their fans. I'm currently geeking out about Svelte, mechanical keyboards, and minimalist UI design, and replaying Ocarina of Time.