我有一个要在运行rails tmp:clear
后运行的任务
namespace :myapp do
task :clear do
# do some stuff
end
end
我了解到可以通过增强该任务来做到这一点:
Rake::Task['tmp:clear'].enhance(['myapp:clear'])
问题是当我的代码加载时,tmp:clear
是未定义的,因此失败:
$ rails tmp:clear
rails aborted!
Don't know how to build task 'tmp:clear' (See the list of available tasks with `rails --tasks`)
myapp/lib/tasks/clear.rake:7:in `<top (required)>'
答案 0 :(得分:2)
在本地任务之后加载任务。您需要require 'rails/tasks'
才能规避该问题。
完整的解决方案:
require 'rails/tasks'
namespace :myapp do
task :clear do
puts "do some stuff"
end
end
Rake::Task['tmp:clear'].enhance(['myapp:clear'])