我正在我的服务器上执行rake任务,但在我的开发计算机上却没有。基本上,我已经能够找到问题到这一行:
yaml = YAML::load(File.open('somefile.yml'))
当我运行rake任务时,我在服务器上收到此错误:
undefined method `keys' for nil:NilClass`
在我的开发机器上,它会完成剩下的代码。我四处搜索但找不到任何关于这个特殊事物的东西,但我觉得这是某种时间问题。
我也试过这个,但我在YAML::load
行中得到了同样的错误:
File.open('somefile.yml', 'r') do |f|
YAML::load(f)
...
end
更新:
这是完整的堆栈跟踪:
[ramon@amplify current]$ rake import:test_blog RAILS_ENV=production --trace
(in /var/www/amplify/releases/20110214164531)
DEPRECATION WARNING: Rake tasks in /var/www/amplify/releases/20110214164531/vendor/plugins/acts_as_containable/tasks/acts_as_containable_tasks.rake are deprecated. Use lib/tasks instead. (called from /var/www/amplify/releases/20110214164531/Rakefile:7)
DEPRECATION WARNING: Rake tasks in /var/www/amplify/releases/20110214164531/vendor/plugins/backup_fu/tasks/backup_fu_tasks.rake are deprecated. Use lib/tasks instead. (called from /var/www/amplify/releases/20110214164531/Rakefile:7)
** Invoke import:test_blog (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute import:test_blog
#<File:/var/www/amplify/releases/20110214164531/test_blog_entries.yml>
rake aborted!
undefined method `keys' for nil:NilClass
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/activerecord-3.0.1/lib/active_record/base.rb:1501:in `attribute_names'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/activerecord-3.0.1/lib/active_record/base.rb:1558:in `attributes'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/activerecord-3.0.1/lib/active_record/attribute_methods.rb:57:in `attribute_method?'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/activemodel-3.0.1/lib/active_model/attribute_methods.rb:394:in `match_attribute_method?'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/activemodel-3.0.1/lib/active_model/attribute_methods.rb:393:in `each'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/activemodel-3.0.1/lib/active_model/attribute_methods.rb:393:in `match_attribute_method?'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/activemodel-3.0.1/lib/active_model/attribute_methods.rb:378:in `respond_to?'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/activerecord-3.0.1/lib/active_record/attribute_methods.rb:52:in `respond_to?'
/home/ramon/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/1.8/yaml.rb:133:in `transfer'
/home/ramon/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/1.8/yaml.rb:133:in `node_import'
/home/ramon/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/1.8/yaml.rb:133:in `load'
/home/ramon/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/1.8/yaml.rb:133:in `load'
/var/www/amplify/releases/20110214164531/lib/tasks/test_import_export.rake:8
/var/www/amplify/releases/20110214164531/lib/tasks/test_import_export.rake:6:in `open'
/var/www/amplify/releases/20110214164531/lib/tasks/test_import_export.rake:6
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/home/ramon/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/rake-0.8.7/bin/rake:31
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/bin/rake:19:in `load'
/home/ramon/.rvm/gems/ree-1.8.7-2010.02@rails3/bin/rake:19
这是rake的任务:
namespace :import do
desc 'Import the entries of test blog'
task :test_blog => :environment do
File.open("#{Rails.root}/test_blog_entries.yml", 'r') do |file|
p file.inspect
yaml = YAML::load(file)
site = Site.find_by_subdomain("test")
blog_page = site.pages.find_by_permalink("blog")
yaml.each do |o|
attr = o.ivars["attributes"]
entry = blog_page.children.new attr
#entry.save
puts %Q(Just created "#{entry.name}" page)
end
end
end
end
namespace :export do
desc 'Export the entries of test blog'
task :test_blog => :environment do
test = Site.find_by_subdomain "kish"
entries = test.pages.find_by_permalink("blog").children
File.open("test_blog_entries.yml", "w") do |f|
f.puts entries.to_yaml
end
end
end
谢谢!
答案 0 :(得分:1)
我发现有类似问题的人。如果导出整个模型the way I did, and the way this guy did,YAML似乎会窒息。所以我只导出了属性,错误就消失了。