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 Rails
module 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:
::Rails.env.production?
Or you can just check the environment variable:
ENV['RAILS_ENV']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With