nil的未定义方法`’:将值推送到数组时为NilClass

我正在尝试用里面的数组创建一个哈希:

# create new hash to store customers and their projects @customers = Hash.new # get all customers from Mite Mite::Customer.all.each do |customer| @customers[customer.id] = {:name => customer.name, :projects => []} end # get all projects from Mite and store them in the :projects array Mite::Project.all.each do |project| @customers[project.customer_id][:projects] << project # line 17 end 

Mite::Project.allMite::Customer.all是外部API(mite.yo.lk)的方法。 他们工作,我得到数据,所以这不是失败。

不幸的是,我必须这样做,因为API没有任何方法来按客户ID过滤项目。

这是错误信息:

 undefined method `[]' for nil:NilClass 

 app/controllers/dashboard_controller.rb:17:in `block in index' app/controllers/dashboard_controller.rb:16:in `each' app/controllers/dashboard_controller.rb:16:in `index' 

我不明白这里有什么不对吗?

@thorstenmüller说的是正确的。 看起来这些项目包含对陈旧客户的引用?

我会建议防御性编码,前提是它不会引入逻辑错误。 就像是:

 # get all projects from Mite and store them in the :projects array Mite::Project.all.each do |project| if @customers[project.customer_id].present? @customers[project.customer_id] << project end end 

同样,如果数据库中的陈旧客户可以接受,我只会建议这样做。

需要注意的另一件事是project.customer_id是一个一致的类型(即总是一个Integer或String):

 # get all customers from Mite Mite::Customer.all.each do |customer| @customers[customer.id.to_i] = {:name => customer.name, :projects => []} end 

 # get all projects from Mite and store them in the :projects array Mite::Project.all.each do |project| @customers[project.customer_id.to_i][:projects] << project # line 17 end 

我个人在这方面被抓了很多次。

HTH和最好的。

如果未定义客户是不可接受的,则可能会引发更具体的错误,该错误会告诉您缺少哪个客户:

 Mite::Project.all.each do |project| begin @customers[project.customer_id].fetch(:projects) << project rescue IndexError => error raise(error.class, "customer missing: #{project.customer_id}", error.backtrace) end end 

另一种方法是创建客户,而不是重新引发错误。