Ruby On Rails: Use Rails.env instead of RAILS_ENV for Rake Custom Tasks for Rails 3

RAILS_ENV returns an empty value in custom rake tasks rails 3.0.0.rc onwards. Instead use Rails.env.

Example Usage:

Lets say you want to determine if you are running the development environment then you could use the following method.

desc "Raise an error unless the RAILS_ENV is development"
task :development_environment_only do
  raise "Hey, development only you monkey!" unless RAILS_ENV == 'development'

Now to ensure your custom method is running in development environment, call the development_environment_only method as a dependency of your rake custom task.

task :abc => [:environment, :development_environment_only] do

If you wish to pass arguments to your rake task, then the code to call the dependencies would be:

task :abc, :argument, :needs => [:environment, :development_environment_only] do |t, args|

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.