views:

45

answers:

1

Hi I know this is something simple I am doing wrong.

I have three tables, installation, neighborhood, schools

Installation:
  has_many :schools
  has_many :neighborhoods
Neighborhood:
  has_many :installations
  has_many :schools
Schools:
  belongs_to :installations
  belongs_to :neighborhoods

I can't figure out how to show the name of the neighborhood the school is located in on the index view. I can get it to show on the show view once I have the school id. But on the index view I can't figure out what to put in the controller that will allow me to access the neighborhood name from the neighborhood_id that is in the School model. I am sure this is so easy and I am screwing up something stupid.

HELP!

A: 

Try:

Installation:
  has_many :schools
  has_many :neighborhoods, :through => :schools
Neighborhood:
  has_many :schools
  has_many :installations, :through => :schools
Schools:
  belongs_to :installations
  belongs_to :neighborhoods

Then you can loop through @school.neighborhoods in your show school view:

<% for neighborhood in @school.neighborhoods %>
  <span><%=h neighborhood.name %></span>
<% end %>

Exchanging the span for whatever HTML output you want.

In the index (Schools index??):

<%=h school.neighborhood.name %>
Reuben Mallaby
Thanks a lot for your help. I had originally done that, but thought there would be an issue with having other join table for those two models (installation and neighborhoods) but it seems to be fine. Thanks again.