in the example below, what is the right way to display the item_category.name attribute within a box controller or view?
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
I am anticipating there is some syntax like this which doesnt work:
<%= @box.item.item_category.name %>
this gives me the following error “undefined method `name’ for nil:NilClass”
Thanks
EDIT:
sorry, the item class contains a belongs_to item_category (not company), I edited the sample.
Since your associations are set up properly, this is probably happening because your
Itemdoesn’t have an associated category, meaning itsitem_category_idfield isnil.