在 rake 任务中寻找仅加载选定模型而不是所有 Rails 环境的方法 [英] Looking for way to load just selected models instead of all Rails environment in rake task
问题描述
在经典的 rake 示例中,我们可以加载所有模型的所有 rails 环境:
in classic rake example we can load all rails environment with all models in taks with:
desc "Pick a last user name"
task :winner => :environment do
puts "Last user: #{User.last.name}"
end
我有一个非常大的 rails 项目,所以加载所有模型和依赖项需要很多时间.有什么方法可以仅使用特定模型或模型集加载导轨?
I have very large rails project so it takes a lot of time to load all models and dependencies. Is any way to load rails with just specific model or set of models?
我即将编写自定义 Rails 加载器,但不想发明自行车;)也许一些 gem 或好的自定义加载程序示例已经存在?
I'm about to write custom rails loader but don't want to invent bycicle ;) Maybe some gem or good custom loader example already exist?
我认为这对 resque 工人的某些情况也很有用.谢谢!
I think it would be useful for some cases of resque workers as well. Thanks!
推荐答案
你可以做 task :winner do
,然后在我们的外部任务中引入模块/模型.
You could do task :winner do
and then require the modules/models in our outside the task.
这篇关于在 rake 任务中寻找仅加载选定模型而不是所有 Rails 环境的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!