What is the purpose of the "environment" in Rake?
According to " Custom Rake Tasks ":
desc "Pick a random user as the winner" task :winner => :environment do puts "Winner: #{pick(User).name}" end As far as I know :winner => :environment means "do environment to winner ". But what is the environment ? When should I use it?
I tried rake -T , but in the list I could not find the environment .
You can access your models and, in fact, your entire environment by setting tasks that depend on the tasks of the environment. This allows you to do something like run rake RAILS_ENV=staging db:migrate .
See " Custom Rake Tasks ".
It loads into the Rails environment, so you can use your models and what not. Otherwise, he has no idea about these things.
So, if you did a task that just did puts "HI!" , you do not need to add the task :environment to the dependencies. But if you want to do something like User.find(1) well, that will be needed.
Enabling => :environment will tell Rake to load the full application environment, providing the appropriate task to access things like classes, helpers, etc. Without :environment , you will not have access to any of these add-ons.
Also => :environment itself does not provide any environment related variables, for example. environment , @environment , RAILS_ENV etc.