什麼是在控制器或視圖中顯示item_category.name屬性的正確方法?在以下示例中顯示父級的父級屬性
class Box < ActiveRecord::Base
has_one :item, :dependent => :destroy
accepts_nested_attributes_for :item, :allow_destroy => true
end
class Item < ActiveRecord::Base
belongs_to :box
belongs_to :item_category
end
class ItemCategory < ActiveRecord::Base
has_many :items
#contains 'name' attribute
end
我期待有這樣它不工作的一些語法:
<%= @box.item.item_category.name %>
這給了我下面的錯誤「未定義的方法'名稱」的零:NilClass」
感謝
編輯: 對不起,該項目類包含belongs_to item_category(不是公司),我編輯了示例。
將代碼發佈到您的控制器中。 – PhillipKregg