Loops - from views to model or controller

I currently have some ugly loops code in my view that I'm trying to
clean up. I'm not sure where to move this code in order to get it all to
work.

The view code looks like this.

<% for a in @model.a %>

  <% @pa = PA.find(a.A_ID) %>
  <% @p = P.find(@pa.P_ID)%>
  <% @pm = PM.find_by_P_ID(@pa.P_ID) %>

  <%# Sample Output %>
  <%=h @pa.P_ID %><br/>
  <%=h @p.Address_1 %>
  <%=h @pm.To %>

<% end %>

The idea is that I first look up the value of "a". This is in my
controller in the show definition.

Then I want to be able to take the value of "a" and look in my "PA"
table to find an ID. Then I want to take that value to look up values
in 2 other tables to produce the output.

Where should I put the queries that are in my view and how would that
code look since I need to use the value that's in "a"?

Thanks!

I currently have some ugly loops code in my view that I'm trying to
clean up. I'm not sure where to move this code in order to get it all to
work.

The view code looks like this.

<% for a in @model.a %>

<% @pa = PA.find(a.A_ID) %>
<% @p = P.find(@pa.P_ID)%>
<% @pm = PM.find_by_P_ID(@pa.P_ID) %>

<%# Sample Output %>
<%=h @pa.P_ID %><br/>
<%=h @p.Address_1 %>
<%=h @pm.To %>

Why can't you just reference them as...

a.pa.P_ID
a.p.address_1
a.pm.To

... in the view? Assuming your associations are setup that seems like the logicial thing to me.

-p