# Undefined Method error

**URL:** <https://www.ruby-forum.com/t/undefined-method-error/156478>\
**Category:** Rails\
**Created:** [December 28, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/undefined-method-error/156478 "2008-12-28T18:05:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@jette](https://www.ruby-forum.com/u/jette)\
**Post date:** [December 28, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/undefined-method-error/156478/1 "2008-12-28T18:05:07Z")

</div>

OK. I have 4 models, item/category/subcategory/brand

association like following

item  
belongs\_to :brands  
belongs\_to :categories  
belongs\_to :subcategories

brand  
has\_many :items

category  
has\_many :items  
has\_many :subcategories

subcategory  
has\_many :items  
belongs\_to :category

I also have “category\_id”, “brand\_id”, “subcategory\_id” as integer, in  
@items

I have following in item/index view

\<% for item in @items %\>  
\<%=h item.category.category\_name %\>  
\<%=h item.brand.brand\_name %\>

I got undefined method “category” error message, but no error message  
for “brand”

In rails console, I tried like following

> > c=Item.find(:first)  
> > c.brand.brand\_name  
> > above I can got the brand\_name correctly. But
> 
> > c.category.category\_name  
> > I got same error message "undefined method ‘category’ "

Why would this happen?

---

<div class="post-metadata">

**Author:** ![jette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@jette](https://www.ruby-forum.com/u/jette)\
**Post date:** [December 28, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/undefined-method-error/156478/2 "2008-12-28T18:12:09Z")

</div>

On 28 Dec 2008, at 17:04, Daniel wrote:

> OK. I have 4 models, item/category/subcategory/brand
> 
> association like following
> 
> item  
> belongs\_to :brands  
> belongs\_to :categories  
> belongs\_to :subcategories

belongs\_to should be singular

Fred

---

<div class="post-metadata">

**Author:** ![jette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@jette](https://www.ruby-forum.com/u/jette)\
**Post date:** [December 28, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/undefined-method-error/156478/3 "2008-12-28T18:16:16Z")

</div>

Hi Fred,  
Sorry that’s my fault.  
in my code actually it’s singular. Because “brand” part works fine 🙂

item  
belongs\_to :brand  
belongs\_to :category  
belongs\_to :subcategory
