我正在尝试创建一个内部包含数组的哈希:
# 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.all
并且Mite::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'
我不明白这里有什么问题?