所以,我实际上有两个问题,但我敢打赌它们是相互关联的。
我必须在旧版应用程序上调用 rake 任务(该任务也是旧版),但遇到“不知道如何构建任务‘环境’”和“未初始化常量”错误。我们正在使用 ruby 2.5.1 在 Rails 5.2 中工作。
rake 文件在lib/tasks/migrations/models.build_stuff.rb
它看起来像这样:
include ActionView::Helpers
namespace :migrations do
namespace :build_stuff do
task build_first_things: :environment do
puts 'Starting building first things'
FirstThings.each { |thing| thing.build! }
puts 'Done!'
end
task build_second_things: :environment do
puts 'Starting building second things'
SecondThings.each { |thing| thing.build! }
puts 'Done!'
end
end
end
因此,我将第一个任务称为:
rake -f ./lib/tasks/migrations/models/build_first_things.rake migrations:build_stuff:build_first_things
在这里,我收到以下错误:
rake aborted!
NameError: uninitialized constant ActionView
我知道,在我的示例中,甚至没有使用 ActionView 助手,在真实文件中,有。
如果仅出于测试原因,我删除了include ActionView::Helpers
引发另一个错误的行:
rake aborted!
Don't know how to build task 'environment' (See the list of available tasks with `rake --tasks`)
我不想将任务称为“环境”,只需要加载应用程序并访问数据库。
有谁知道,为什么会发生任何一种(或)两种情况?
非常非常感谢你 :)
*** 解决方案 ***
它在调用bin/rails
或bundle exec rake
而不是简单地工作时起作用rake
。