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?


I think it should be

belongs_to :category

in your Classified model.

Scott B.
Electro Interactive, Inc.
Office: 813-333-5508

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



This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs