我正在编写一个rake任务,它一度使用自定义YAML文件导入方法来为数据库设定种子。
rake任务看起来像:
desc "Seed the database with production/ data."
task :production => :environment do
import_yaml 'seed/production'
end
在导入代码的某一点上,我有:
ActiveRecord::Base.transaction do
尝试运行rake任务抛出:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
堆栈跟踪指向代码中的上述行。
有没有办法在rake任务期间实例化ActiveRecord :: Base?
谢谢!
答案 0 :(得分:3)
也许你没有在rake任务中包含'环境'?
rake task can't access rails.cache
task (:my_rake_task => :environment) do
# ....
end
没有=> :您将无法访问任何rails应用程序模型的环境