简体   繁体   English

Rake 中的“环境”任务是什么?

[英]What's the 'environment' task 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, the :winner =>:environment means "do environment before winner ".据我所知, :winner =>:environment的意思是“在winner之前做environment ”。 But what's environment ?但是什么是environment When should I use it?我应该什么时候使用它?

I tried rake -T , but in the list I couldn't find environment .我试过rake -T ,但在列表中我找不到environment

You can get access to your models, and in fact, your whole environment by making tasks dependent on the environment task.您可以通过使任务依赖于环境任务来访问您的模型,实际上是您的整个环境。 This lets you do things like run rake RAILS_ENV=staging db:migrate .这使您可以执行诸如run rake RAILS_ENV=staging db:migrate之类的操作。

See " Custom Rake Tasks ".请参阅“自定义 Rake 任务”。

It loads in your Rails environment so you can actually use your models and what not.它加载到您的 Rails 环境中,因此您可以实际使用您的模型以及其他什么。 Otherwise, it has no idea about those things.否则,它对那些事情一无所知。

So if you made a task that just did puts "HI!"因此,如果您做了一项刚刚完成的任务,就会发出puts "HI!" then you don't need to add the :environment task to the dependencies.那么您不需要将:environment任务添加到依赖项中。 But if you wish to do something like User.find(1) well that will need it.但是,如果您想很好地执行User.find(1)之类的操作,那将需要它。

Including =>:environment will tell Rake to load full the application environment, giving the relevant task access to things like classes, helpers, etc. Without the :environment , you won't have access to any of those extras.包括=>:environment将告诉 Rake 加载完整的应用程序环境,使相关任务可以访问类、助手等。没有:environment ,您将无法访问任何这些额外内容。

Also =>:environment itself does not make available any environment-related variables, eg environment , @environment , RAILS_ENV , etc.此外=>:environment本身不提供任何与环境相关的变量,例如environment@environmentRAILS_ENV等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM