在.each循环中Rails 3 nil对象错误

时间:2011-08-12 13:09:32

标签: ruby-on-rails ruby loops

我在rake任务中有以下代码,并且在循环开始运行的点附近我一直遇到错误“你有一个nil对象,当你没想到它”。我只是四处转转。我在这个循环中做错了什么?

task :getmusicbrainz => :environment do
  include HTTParty
  puts "Starting Musicbrainz matching..."
  releases = Release.all
  releases.each do |release|
    name = release['artist']
    artistname = CGI::escape(release['artist'])
    puts "Encoded #{name} into #{artistname}"
    request_id = HTTParty.get("http://www.musicbrainz.org/ws/2/artist?query=#{artistname}&limit=1")
    count = request_id['metadata']['artist_list']['count']
    puts "Querying MusicBrainz for the ID yo!"

    if count == "0"
      musicbrainz_id = "null"
      puts "No brainz id available"
    else
      musicbrainz_id = request_id['metadata']['artist_list']['artist']['id']
      puts "Brainz ID Found: #{musicbrainz_id}"
    end

    Release.update!(:artist_musicbrainz_id => musicbrainz_id)
    puts "Record updated!"
    sleep 2
  end
  puts "Musicbrainz matching complete!"
end

更新 - 使用--trace运行任务给了我:

** Invoke getmusicbrainz (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute getmusicbrainz
Starting Musicbrainz matching...
rake aborted!
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/httparty-0.7.8/lib/httparty /module_inheritable_attributes.rb:19:in `inherited'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.7/lib/active_record/log_subscriber.rb:2:in `<module:ActiveRecord>'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.7/lib/active_record/log_subscriber.rb:1:in `<top (required)>'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `block in require'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.7/lib/active_record/base.rb:24:in `<top (required)>'
/Users/martyndavies/code/piasapi/app/models/release.rb:1:in `<top (required)>'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:454:in `load'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:454:in `block in load_file'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:453:in `load_file'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:340:in `require_or_load'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:491:in `load_missing_constant'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:183:in `block in const_missing'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:in `each'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:in `const_missing'
/Users/martyndavies/code/piasapi/lib/tasks/adder.rake:7:in `block in <top (required)>'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/task.rb:205:in `block in execute'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/martyndavies/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/application.rb:112:in `invoke_task'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/application.rb:90:in `block (2 levels) in top_level'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/application.rb:90:in `each'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/application.rb:90:in `block in top_level'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/application.rb:84:in `top_level'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/application.rb:62:in `block in run'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/lib/rake/application.rb:59:in `run'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/bin/rake:32:in `<top (required)>'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/bin/rake:19:in `load'
/Users/martyndavies/.rvm/gems/ruby-1.9.2-p180/bin/rake:19:in `<main>'
Tasks: TOP => getmusicbrainz

0 个答案:

没有答案