Ruby-on-rails Rake 中的“环境”任务是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7044714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the 'environment' task in Rake?
提问by Lai Yu-Hsuan
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 => :environmentmeans "do environmentbefore winner". But what's environment? When should I use it?
据我所知,:winner => :environment意思是“做environment之前winner”。但什么是environment?我应该什么时候使用它?
I tried rake -T, but in the list I couldn't find environment.
我试过了rake -T,但在列表中我找不到environment。
回答by Sameer C
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 任务”。
回答by MrDanA
It loads in your Rails environment so you can actually use your models and what not. Otherwise, it has no idea about those things.
它加载到您的 Rails 环境中,因此您可以实际使用您的模型,而不能使用其他模型。否则,它对这些事情一无所知。
So if you made a task that just did puts "HI!"then you don't need to add the :environmenttask to the dependencies. But if you wish to do something like User.find(1)well that will need it.
因此,如果您创建了一个刚刚完成的任务,puts "HI!"则无需将该:environment任务添加到依赖项中。但是,如果你想做一些类似的事情,那就User.find(1)需要它了。
回答by Lars Levie
Including => :environmentwill 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 => :environmentitself does not make available any environment-related variables, e.g. environment, @environment, RAILS_ENV, etc.
另外=> :environment本身并没有提供任何与环境有关的变量,例如environment,@environment,RAILS_ENV等。

