我有一个 Rails 任务:我应该使用 script/runner 还是 rake? [英] I have a Rails task: should I use script/runner or rake?

查看:45
本文介绍了我有一个 Rails 任务:我应该使用 script/runner 还是 rake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于ad hoc Rails 任务,我们有几个实现替代方案,其中主要的似乎是:

For ad hoc Rails tasks we have a few implementation alternatives, chief among which would seem to be:

script/runner some_useful_thing

和:

rake some:other_useful_thing

我应该更喜欢哪个选项?如果有一个明显的最爱,那么我什么时候应该考虑使用另一个?如果从来没有,那么您为什么会认为它仍然存在于框架中而没有弃用警告?

Which option should I prefer? If there's a clear favourite then when, if ever, should I consider using the other? If never, then why would you suppose it's still present in the framework without deprecation warnings?

推荐答案

它们之间的区别在于 script/runner 会引导 Rails 而 Rake 任务不会,除非您通过使任务依赖于:environment,像这样:

The difference between them is that script/runner boots Rails whereas a Rake task doesn't unless you tell it to by making the task depend on :environment, like this:

task :some_useful_task => :environment do
  # do some useful task
end

由于启动 Rails 很昂贵,如果可以避免的话,可能值得跳过.

Since booting Rails is expensive, it might be worth skipping if you can avoid it.

除此之外,它们大致相同.我两者都使用,但最近我更多地使用 script/runner 单独执行脚本.

Other than that, they are roughly equivalent. I use both, but lately I've used script/runner executing a script separately more.

这篇关于我有一个 Rails 任务:我应该使用 script/runner 还是 rake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆