# Model problem?

**URL:** <https://www.ruby-forum.com/t/model-problem/125584>\
**Category:** Rails\
**Created:** [January 10, 2008, 11:23am UTC](https://www.ruby-forum.com/t/model-problem/125584 "2008-01-10T11:23:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b77776/32.png) [@carlox](https://www.ruby-forum.com/u/carlox)\
**Post date:** [January 10, 2008, 11:23am UTC](https://www.ruby-forum.com/t/model-problem/125584/1 "2008-01-10T11:23:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![carlox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b77776/32.png) [@carlox](https://www.ruby-forum.com/u/carlox)\
**Post date:** [January 10, 2008, 11:25am UTC](https://www.ruby-forum.com/t/model-problem/125584/2 "2008-01-10T11:25:27Z")

</div>

did you check in the db, that the product record has a valid category\_id  
?

---

<div class="post-metadata">

**Author:** ![carlox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b77776/32.png) [@carlox](https://www.ruby-forum.com/u/carlox)\
**Post date:** [January 10, 2008, 6:22pm UTC](https://www.ruby-forum.com/t/model-problem/125584/3 "2008-01-10T18:22:11Z")

</div>

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-
