超过 3 小时我试图解决非常简单的错误(乍一看):
undefined method `empty?' for nil:NilClass
但仍然没有成功。
我有 DB 表products
,其中包含列category_id
和manufacturer_id
.
协会:
class Product < ActiveRecord::Base
belongs_to :manufacturer
belongs_to :category
...
end
class Category < ActiveRecord::Base # the same for Manufacturer
has_ancestry
has_many :products
end
试图获取一些数据:
Product.where('category_id IS NOT NULL AND manufacturer_id IS NOT NULL').each do |product|
...
puts product.manufacturer.name # here's the error
puts product.category.name # here's the error
...
end
我获取了所有行,列中的值不是 NILmanufacturer_id
并且category_id
......那么我怎样才能得到这个错误?
另外,我试过:
...
puts product.manufacturer.name unless product.manufacturer_id.nil?
puts product.category.name unless product.category_id.nil?
...
我究竟做错了什么?