2009-11-21 5 views
0

J'ai actuellement une table de recherche simple. Lorsque j'affiche un agency, je voudrais afficher le locality.name plutôt que le locality_id. Le Locality.name est stocké dans le tableau locality. Juste le id est stocké dans le tableau agency.RoR: clé étrangère de base question

Ci-dessous montre le locality_id, je voudrais qu'il montre le locality_name à la place.

Comment faire?

contrôleur agence

def index 
    @agencies = Agency.all 
    respond_to do |format| 
    format.html # index.html.erb 
    format.xml { render :xml => @agencies } 
    end 
end 
index agence

<% @agencies.each do |agency| %> 
    <p> 
    <b>Agency:</b> 
    <%=h @agency.agency %> 
    </p> 
    <p> 
    <b>Locality:</b> 
    <%=h @agency.locality_id %> 
    </p> 
<% end %> 

Encore une fois, je sais que cela est une question fondamentale donc j'apprécie l'aide

Répondre

2

changer juste dans votre vue.

<% @agencies.each do |agency| %> 

<p> 
<b>Agency:</b> 
<%=h @agency.agency %> 
</p> 

<p> 
<b>Locality:</b> 
<%=h @agency.locality.name %> <!-- this will bomb out if there agencies that 
            don't have a locality - if that's an issue, 
            add "if @agency.locality" to the end. --> 
</p> 
<% end %>