I’m working on a rails gem, in which I have some logic that I’d like to be conditional based on the rails environment.
The following code errors out:
if Rails.env.production?
When running in the test app this gives me:
undefined method .env for Gemname::Rails::Module
So, how do you find the Rails environment from a method call in a module that’s in a gem?
You have a
Railsmodule in your project, and the constant lookup is finding it, rather than the top-level Rails module. You can either use the top-level constant:Or you can just check the environment variable: