3時間以上私はかなり簡単なエラーを解決しようとしています(一見):
undefined method `empty?' for nil:NilClass
しかし、それでも成功しません。
products
列category_id
とを含むDBテーブルがあります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
すべての行をフェッチしましたが、列にNIL値がありませんmanufacturer_id
...ではcategory_id
、どうすればこのエラーを取得できますか?
また、私は試しました:
...
puts product.manufacturer.name unless product.manufacturer_id.nil?
puts product.category.name unless product.category_id.nil?
...
私は何が間違っているのですか?