Search code examples
ruby-on-rails-5rails-activerecord

EnvironmentMismatchError - Rails.env being set to "production"


I'm receiving a Missingsecret_key_basefor 'production' environment, set this value in config/secrets.yml error when checking localhost. This doesn't make sense, since I should be in development.

I logged Rails.env during the server start, and it is showing development.

From there I tried some database stuff:

  • rails dbconsole gets me this error: psql: FATAL: role "my-server" does not exist

  • rails db:create gets me this:

Database 'my-server_development' already exists

Database 'my-server_test' already exists

  • Next I decided to try rails db:create, which gave me the most indicative error:

ActiveRecord::EnvironmentMismatchError: You are attempting to modify a database that was last run in development environment. You are running in production environment. If you are sure you want to continue, first set the environment using:

bin/rails db:environment:set RAILS_ENV=production

In conclusion, some mystery config in my environment seems to be set to production. This error is suggesting that I try to set my rails env to production to match it, but what I want is to change that mystery something to development.

Any help would be greatly appreciated.


Solution

  • Okay figured it out. I had a typo in one of my initializers and was checking my Rails env like so:

    if Rails.env = "production"
      # blahblah do something
    

    This of course was setting Rails.env to "production". I messing around with Pry in the Rails startup process, and I figured out that Rails.env was returning "development" at one point, and "production" later on.

    From there, a quick search found the problem. Thanks to everyone who took the time to look at this. I'll update the title to something more indicative of the problem.