I'm making a rake task that fails on my server, but not on my development computer. Essentially, I've been able to pinpoint the problem to this line:
yaml = YAML::load(File.open('somefile.yml'))
When I run the rake task, I get this error on the server:
undefined method `keys' for nil:NilClass`
On my dev machine it goes through the rest of the code fine. I searched around but couldn't find anything about this particular thing, but I have a feeling it's a timing issue of some sort.
I've also tried this, but I get the same error in the YAML::load
line:
File.open('somefile.yml', 'r') do |f|
YAML::load(f)
...
end
- If it is a timing issue: how do I wait for the file to load before I make YAML load it?
- If it's not a timing issue: what might be the problem?
UPDATE:
Here's the full stack trace:
[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
And here's the rake task:
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
Thanks!
I found someone with a similar problem. It seems that YAML chokes if you export the whole model the way I did, and the way this guy did. So I exported the attributes only and the error went away.
精彩评论