Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine rails environment from a gem

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?

like image 870
Andrew Avatar asked Jan 15 '23 08:01

Andrew


1 Answers

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']
like image 171
Chris Heald Avatar answered Jan 24 '23 17:01

Chris Heald