只为 rake 任务加载部分环境? [英] Load only part of environment for rake task?

查看:55
本文介绍了只为 rake 任务加载部分环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一堆与加载相关的文章一个任务的整个 Rails 环境.但是,我担心这是不必要的,因为我的任务只使用两个模型(加上resque"和resque/scheduler").如何只为我的任务加载环境的某些部分?

I've seen a bunch of articles relating to loading the entire Rails environment for a task. However, I'm concerned that this is unnecessary because I only use two models for my task (plus the 'resque' and 'resque/scheduler'). How can I only load certain parts of an environment for my task?

另外,这个 问题 似乎是一样的,但它已经过时了,似乎没有人充分回答它......我现在提前问,有人请提供代码而不仅仅是解释.

Also, this question seems to be the same, but it's gotten old and no one seems to have sufficiently answered it... I'll ask now, ahead of time, that someone please give code rather than just an explanation.

推荐答案

你可能不会喜欢这个答案,但是...

You probably won't like this answer, but...

您不应只加载环境的一部分.任何解决方法都将是丑陋、令人不快和脆弱的.仅需要您的整个环境就更快、更容易、更标准.很可能您提出的任何解决方案都只会使任务的启动时间缩短一两秒钟,而且您为实现它而投入的时间和精力根本不值得.

You shouldn't load only part of your environment. Any workaround will be ugly and unpleasant and brittle. It's faster, easier, and more standard just to require your entire environment. Likely any solution you come up with will only shave one or two seconds off the startup time of the task, and it just won't be worth it for how much time and energy you've invested in making it happen.

然而,如果你真的想这样做,如果你只加载 ActiveRecord 模型,你可以在你的任务之前尝试这样的事情:

Nevertheless, if you really want to do this, if you're only loading ActiveRecord models you can try something like this before your task:

require 'active_record'
require './app/models/my_model.rb'

您可能会收到一堆关于未定义方法和缺少常量的错误.您可以手动更正其中的每一个,需要一个一个的文件来更正问题,或者只是接受我的建议并需要您的环境.(提示:只需要你的环境.)

You'll likely get a bunch of errors about undefined methods and missing constants. You can manually correct each one of those, requiring files one by one to correct the issue, or just take my advice and require your environment. (Hint: just require your environment.)

这篇关于只为 rake 任务加载部分环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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