什么是“环境”? Rake 中的任务?
根据“自定义 Rake 任务”:
desc "Pick a random user as the winner"
task :winner => :environment do
puts "Winner: #{pick(User).name}"
end
据我所知,:获胜者=> :environment
表示“在 winner
之前执行 environment
”。但什么是环境
?我应该什么时候使用它?
我尝试了rake -T
,但在列表中我找不到环境
。
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
". But what's environment
? When should I use it?
I tried rake -T
, but in the list I couldn't find environment
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过使任务依赖于环境任务,您可以访问您的模型,事实上,您的整个环境。这可以让您执行诸如
run rake RAILS_ENV=staging db:migrate
之类的操作。请参阅“自定义 Rake 任务”。
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
.See "Custom Rake Tasks".
它会加载到您的 Rails 环境中,以便您可以实际使用您的模型以及其他什么。否则,它对这些事情一无所知。
因此,如果您创建的任务只是
puts“HI!”
,那么您不需要将:environment
任务添加到依赖项中。但是,如果您希望做类似User.find(1)
的事情,那么就需要它。It loads in your Rails environment so you can actually use your models and what not. Otherwise, it has no idea about those things.
So if you made a task that just did
puts "HI!"
then you don't need to add the:environment
task to the dependencies. But if you wish to do something likeUser.find(1)
well that will need it.包括
=>; :environment
将告诉 Rake 加载完整的应用程序环境,为相关任务提供对类、助手等的访问权限。如果没有:environment
,您将无法访问任何内容这些额外的东西。还有
=>; :environment
本身不提供任何与环境相关的变量,例如environment
、@environment
、RAILS_ENV
等。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.Also
=> :environment
itself does not make available any environment-related variables, e.g.environment
,@environment
,RAILS_ENV
, etc.