Felix
Felix

Reputation: 5619

Rails environment how to find out in code?

are there any solutions how I can find out in which environment my application is running?

I have some rake calls wich need to have the environment like this:

system "RAILS_ENV=development rake crons:dosomething"

But this should not only work in development I want to use this in test production and development?

how can I do this?

second task where I need this is in config/schedule.rb the should also run in all environments

rake "crons:dosomething", :environment => :development
rake "crons:dosomething", :environment => :production

this dosn't work

Upvotes: 1

Views: 957

Answers (2)

dandlezzz
dandlezzz

Reputation: 613

You can also do

Rails.env.production? || Rails.env.development?

with any of the environments you have.

Alternatively to pass an environment to a rake task you can do this

rake "crons:dosomething RAILS_ENV=production"

Upvotes: 1

errata
errata

Reputation: 26802

You can match it to strings like:

if Rails.env.eql?('production')
  #Do production things
end 

Upvotes: 1

Related Questions