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 won’t happen in production if you’re using PostgreSQL as your database, but it’s still annoying when you’re trying to develop an application. The issue can be fixed by changing a single line inside puma.rb:

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

# After
threads_count = 1

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

If you’re here from my Apartment tutorial, click here to return.

Alternative method: You can also give the server a few seconds to switch over to the correct subdomain. You may have to refresh the page several times to get this to work and deal with error messages so I don’t recommend it.