Quick and Easy Associations Question

Hello everyone. I trying to learn associations, but have a quick question that shouldn't be too hard.

I have a classifieds table that has a column category_id and I have a categories table. I put has_many :classifieds in the Category model and belongs_to :categories in the Classifieds model

I am trying to get the category for a classified from the view. What is the correct way to do this? I thought it would be something like:

<% for classified in @classifieds %>      <%= classified.category.name %> <% end %>

but obviously it isn't working. What is the right way to do this? Thanks!


I think it should be

belongs_to :category

in your Classified model.

Yes, the "belongs_to" part should be singular while the "has_many" part should be plural. Check this for a good example:

   Radar – O’Reilly

Cheers, Daniel.