简体   繁体   中英

Rails - NoMethodError: undefined method `each' for nil:NilClass on Model.find_by

This error occurs only in a rake, and not when run in rails console.

Here is the code that is crashing:

task upload_activities: :environment do
    include GoogleHelper
    file = 'upload/things_to_do_activities_upload_v1.csv'
    content = File.read(file)
    csv = CSV.parse(content, headers: true)
    puts 'Parsed CSV successfully'
    puts csv[0].to_hash # This works

    csv.each do |row|
      puts "1" # this is output
      guide = ThingsToDoGuide.find_by(city_id: 4321) # crashes here
      puts "2"
      ....

When running the rake, the console outputs everything until the line ThingsToDoGuide.find_by(city_id: 4321) .

I can run the exact same line in rails console and it works fine.

The error text is:

rails aborted!
NoMethodError: undefined method `each' for nil:NilClass
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/httparty-0.15.5/lib/httparty/module_inheritable_attributes.rb:38:in `inherited'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.4/lib/active_record/associations/collection_proxy.rb:30:in `<module:Associations>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.4/lib/active_record/associations/collection_proxy.rb:2:in `<module:ActiveRecord>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.4/lib/active_record/associations/collection_proxy.rb:1:in `<top (required)>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:293:in `require'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:293:in `block in require'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:257:in `block in load_dependency'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:662:in `new_constants_in'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:257:in `load_dependency'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:293:in `require'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/will_paginate-3.1.6/lib/will_paginate/active_record.rb:244:in `<module:ActiveRecord>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/will_paginate-3.1.6/lib/will_paginate/active_record.rb:18:in `<module:WillPaginate>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/will_paginate-3.1.6/lib/will_paginate/active_record.rb:6:in `<top (required)>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:293:in `require'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:293:in `block in require'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:257:in `block in load_dependency'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:662:in `new_constants_in'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:257:in `load_dependency'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:293:in `require'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/will_paginate-3.1.6/lib/will_paginate/railtie.rb:10:in `block (2 levels) in <class:Railtie>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/lazy_load_hooks.rb:43:in `instance_eval'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/lazy_load_hooks.rb:43:in `execute_hook'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/lazy_load_hooks.rb:49:in `each'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.4/lib/active_record/base.rb:324:in `<module:ActiveRecord>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.4/lib/active_record/base.rb:24:in `<top (required)>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:293:in `require'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:293:in `block in require'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:257:in `block in load_dependency'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:662:in `new_constants_in'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:257:in `load_dependency'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:293:in `require'
/Users/Calvino/dev/triphappy/app/models/application_record.rb:3:in `<top (required)>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:477:in `load'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:477:in `block in load_file'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:662:in `new_constants_in'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:476:in `load_file'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:375:in `block in require_or_load'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:37:in `block in load_interlock'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies/interlock.rb:12:in `block in loading'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/concurrency/share_lock.rb:150:in `exclusive'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies/interlock.rb:11:in `loading'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:37:in `load_interlock'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:358:in `require_or_load'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:511:in `load_missing_constant'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:203:in `const_missing'
/Users/Calvino/dev/triphappy/app/models/things_to_do_guide.rb:1:in `<top (required)>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:477:in `load'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:477:in `block in load_file'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:662:in `new_constants_in'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:476:in `load_file'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:375:in `block in require_or_load'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:37:in `block in load_interlock'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies/interlock.rb:12:in `block in loading'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/concurrency/share_lock.rb:150:in `exclusive'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies/interlock.rb:11:in `loading'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:37:in `load_interlock'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:358:in `require_or_load'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:511:in `load_missing_constant'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/dependencies.rb:203:in `const_missing'
/Users/Calvino/dev/triphappy/lib/tasks/things_to_do.rake:81:in `block (3 levels) in <top (required)>'
/Users/Calvino/dev/triphappy/lib/tasks/things_to_do.rake:76:in `block (2 levels) in <top (required)>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/railties-5.0.4/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/railties-5.0.4/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/railties-5.0.4/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/railties-5.0.4/lib/rails/commands.rb:18:in `<top (required)>'
/Users/Calvino/dev/triphappy/bin/rails:9:in `require'
/Users/Calvino/dev/triphappy/bin/rails:9:in `<top (required)>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/Users/Calvino/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/Users/Calvino/dev/triphappy/bin/spring:15:in `require'
/Users/Calvino/dev/triphappy/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => things_to_do:upload_activities
(See full trace by running task with --trace)

I figured there is a problem with the model, but it is completely blank.

class ThingsToDoGuide < ApplicationRecord

end

Any thoughts?

This looks like an issue with the HTTParty gem, not Rails. If you go to the inherited method at line 38 of httparty/lib/module_inheritable_attributes.rb as suggested by the stack trace you'll see the each method being called on an instance variable. That instance variable must be nil in that context for some reason. You might consider opening an issue in the repo.

https://github.com/jnunemaker/httparty/blob/master/lib/httparty/module_inheritable_attributes.rb#L38

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM