Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if my rails is in the development environment and not the test environment?

I have some code that needs to run only if the rails app is in the development environment (i.e. $ rails server) but not in the test environment (i.e. $ rake test).

When I try

if Rails.env.development?     dont run me during testing end 

the code gets executed regardless of which environment I am in. I've even tried:

if Rails.env.development? and not Rails.env.test?     NO, REALLY, DONT RUN ME DURING TESTING end 

but no love.

What should I be doing instead?

like image 492
spierepf Avatar asked Mar 18 '13 18:03

spierepf


People also ask

How do I check environment in Ruby?

Ruby has direct access to environment variables via the ENV hash. Environment variables can be directly read or written to by using the index operator with a string argument.

How do I see environment variables in Rails?

Use command ENV in rails console. That will return a hash of your environmental values you can access. Alternatively, you can access your environmental variables from your apps root path using the same command and the variables will be returned formatted.

What environments Does Rails have by default?

When we generate a new Rails application we get three environments by default: development , test and production . There's nothing special about these particular environments, though, and there are very few places in the Rails source code that refer to them.


1 Answers

It looks like you're calling it correctly. Perhaps the problem is that the environment is named differently somewhere. Try in the console:

> Rails.env => "development" > Rails.env.development? => true > Rails.env.test? => false 

...to confirm that the environment is what you think it is.

like image 132
Mori Avatar answered Oct 13 '22 06:10

Mori