时间问题?加载 yml 文件:未定义方法“keys”;对于 nil:NilClass

发布于 2024-10-19 07:45:22 字数 5812 浏览 6 评论 0原文

我正在执行一个 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
  • 如果是计时问题:如何等待文件在让 YAML 加载之前加载?
  • 如果不是时间问题:可能是什么问题?

更新:

这是完整的堆栈跟踪:

[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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱着落日 2024-10-26 07:45:22

我发现有人有类似的问题。如果导出整个模型,YAML 似乎会阻塞 我就是这么做的,这家伙也是这么做的。所以我只导出属性,错误就消失了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文