rake db:seed和mongoid的未初始化的常量错误

时间:2013-01-06 07:07:49

标签: ruby-on-rails mongoid

当我尝试将数据存入我的应用程序时,我收到此错误。

我有一个看似......的JSON文件。

[{"code": "OCH", "name": "blah", "location": "Texas, United States"},…

我的db:种子看起来像:

Airport.delete_all
airport_codes = JSON.parse(File.read(Rails.root.join('db/airport_codes.json')))
airport_codes.each do |code|
  Airport.create!(code)
end

我的堆栈跟踪:

/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@swlv/gems/mongoid-3.0.16/lib/mongoid/railties/database.rake:13:in `block (2 levels) in <top (required)>'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/task.rb:228:in `call'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/task.rb:228:in `block in execute'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/task.rb:223:in `each'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/task.rb:223:in `execute'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/task.rb:166:in `block in invoke_with_call_chain'
/Users/nicholasshook/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_with_call_chain'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/task.rb:152:in `invoke'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/application.rb:143:in `invoke_task'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/application.rb:101:in `each'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/application.rb:101:in `block in top_level'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/application.rb:110:in `run_with_threads'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/application.rb:95:in `top_level'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/application.rb:73:in `block in run'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/lib/rake/application.rb:70:in `run'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-10.0.3/bin/rake:33:in `<top (required)>'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `load'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `<main>'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@swlv/bin/ruby_noexec_wrapper:14:in `eval'
/Users/nicholasshook/.rvm/gems/ruby-1.9.3-p194@swlv/bin/ruby_noexec_wrapper:14:in `<main>'

谢谢!

1 个答案:

答案 0 :(得分:1)

希望你已经像这样创建了你的rake任务

task :<rake task name> => :environment do
   Airport.delete_all
   airport_codes = JSON.parse(File.read(Rails.root.join('db/airport_codes.json')))
   airport_codes.each do |code|
     Airport.create!(code)
   end
end

如果这也是正确的,那么尝试将airport.rb包含在您的rake文件中

require 'airport.rb'

,但据我所知,rake任务格式应该适合你

HTH