简体   繁体   English

在rake任务中使用ActiveRecord :: Base.transaction?

[英]Using ActiveRecord::Base.transaction in a rake task?

I am writing a rake task which, at one point, uses a custom YAML file import method to seed the database. 我正在编写一个rake任务,该任务有时使用一种自定义的YAML文件导入方法来播种数据库。

The rake task looks like: 耙任务看起来像:

desc "Seed the database with production/ data."
task :production => :environment do     
  import_yaml 'seed/production'
end

At one point in the import code, I have: 在导入代码的某一点上,我有:

ActiveRecord::Base.transaction do

Trying to run the rake task throws: 尝试运行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.[]

The stack trace points to the aforementioned line in the code. 堆栈跟踪指向代码中的上述行。

Is there a way to instantiate ActiveRecord::Base during a rake task? 有没有办法在瑞克任务期间实例化ActiveRecord :: Base?

Thanks! 谢谢!

maybe you have not included the 'environment' in the rake task? 也许您没有在rake任务中包含“环境”?

rake task can't access rails.cache 耙任务无法访问rails.cache

task (:my_rake_task => :environment) do
    # ....
end

without the => :environment you will not have access to any of your rails app models 没有=>:environment,您将无法访问任何Rails应用程序模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM