Forum: Ruby on Rails Undefined Method error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3949b43a989430f9959e816e105cbbcc?d=identicon&s=25 Jette Chan (jette)
on 2008-12-28 18:05
(Received via mailing list)
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?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-28 18:12
(Received via mailing list)
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
3949b43a989430f9959e816e105cbbcc?d=identicon&s=25 Jette Chan (jette)
on 2008-12-28 18:16
(Received via mailing list)
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
This topic is locked and can not be replied to.