开发者

Using ActiveRecord::Base.transaction in a rake task?

开发者 https://www.devze.com 2023-01-01 11:56 出处:网络
I am writing a rake task which, at one point, uses a custom YAML file import method to seed the database.

I am writing a rake task which, at one point, uses a custom YAML file import method to seed the database.

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:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The er开发者_如何学运维ror 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?

Thanks!


maybe you have not included the 'environment' in the rake task?

rake task can't access rails.cache

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

without the => :environment you will not have access to any of your rails app models

0

精彩评论

暂无评论...
验证码 换一张
取 消