# Newbie: has\_many

**URL:** <https://www.ruby-forum.com/t/newbie-has-many/160696>\
**Category:** Rails\
**Created:** [February 18, 2009, 11:23pm UTC](https://www.ruby-forum.com/t/newbie-has-many/160696 "2009-02-18T23:23:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterkemp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@peterkemp](https://www.ruby-forum.com/u/peterkemp)\
**Post date:** [February 18, 2009, 11:23pm UTC](https://www.ruby-forum.com/t/newbie-has-many/160696/1 "2009-02-18T23:23:49Z")

</div>

I am sorry for the newbie question, but I am really stuck and I can’t  
work it out. ☹ Have 2 models. category and product (has\_many and  
belongs\_to).

My routes.rb:  
map.resources :categories do |category|  
category.resources :products  
end

index in category controller:  
def index  
@category = Category.find(params[:id])  
@products = @category.products  
end

and then in my index.rhtml:

\<% for product in @products %\>  
\<%= product.title %\>…  
\<% end %\>

it will end up with this error:  
undefined method `title’ for #Class:0x54eb998

I am sure the title is column name in my table. I can create a new  
product, edit it, delete it, but I am not able to show the data  
properly.

Can anyone help me? Thanks in advance.  
Peter

---

<div class="post-metadata">

**Author:** ![peterkemp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@peterkemp](https://www.ruby-forum.com/u/peterkemp)\
**Post date:** [February 18, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/newbie-has-many/160696/2 "2009-02-18T23:54:18Z")

</div>

On Feb 18, 2009, at 5:23 PM, Peter Killik wrote:

> def index  
> @category = Category.find(params[:id])  
> @products = @category.products  
> end
> 
> and then in my index.rhtml:
> 
> \<% for product in @products %\>  
> \<%= product.title %\>…  
> \<% end %\>

Read:  
[http://blog.grayproductions.net/articles/the\_evils\_of\_the\_for\_loop](http://blog.grayproductions.net/articles/the_evils_of_the_for_loop)  
and change to:

\<% @products.each do |product| %\>

> it will end up with this error:  
> undefined method `title’ for #Class:0x54eb998
> 
> I am sure the title is column name in my table. I can create a new  
> product, edit it, delete it, but I am not able to show the data  
> properly.
> 
> Can anyone help me? Thanks in advance.  
> Peter

You’re saying/showing CategoryController, but do you mean  
ProductsController?

Since the undefined ‘title’ is attributed to #Class:... and not  
#Product:... do you have a naked title somewhere that’s being  
interpreted as a method call on the anonymous view class? Is that the  
correct line from the view?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![peterkemp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@peterkemp](https://www.ruby-forum.com/u/peterkemp)\
**Post date:** [February 19, 2009, 8:24am UTC](https://www.ruby-forum.com/t/newbie-has-many/160696/3 "2009-02-19T08:24:57Z")

</div>

Rob B. wrote:

> Read:  
> [http://blog.grayproductions.net/articles/the\_evils\_of\_the\_for\_loop](http://blog.grayproductions.net/articles/the_evils_of_the_for_loop)  
> and change to:
> 
> \<% @products.each do |product| %\>

Thx Rob, I changed the for\_loop and it works. THnaks.  
Peter
