Model problem?

hello folks, i’m playing around with RoR and let’s say I have 2 tables
in ny DB: products and categories. each product must have a category.
i can associate the category_id field in products table with the
different categories that already exist (i used scaffolding). what i
cannot do -and i can’t see where the problem is- is to retrieve the
category name when i’m displaying my product info.

in my app/views/product/list.rhtml i have:

<td><%=h product.title %></td>
<td><%=h product.category.name %></td>
<td><%=h product.price %></td>
<td><%=h product.stock %></td>

and it keeps saying ‘You have a nil object when you didn’t expect it!’
as I want to get product.category.name

my models are:

class Product < ActiveRecord::Base
belongs_to :category
end

class Category < ActiveRecord::Base
has_many :products
end

the most strange thing of this is that in app/views/product/
_form.rhtml i have:

<%= collection_select :product, :category_id, @categories, :id, :name
%>

and the combo fills up with the categories name!

what am i doing wrong?

did you check in the db, that the product record has a valid category_id
?

yes, it has a valid numerical category_ID that matches what i select
in the combo box, actually i can display that ID, but i cannot get
that category’s name

On Jan 10, 4:25 am, Thorsten M. <rails-mailing-l…@andreas-