Reputation: 3938
How do you find the record name of a foreign key in a rails view?
For example, I have a view that lists the following:
<% @cycles.each do |cycle| %>
<tr>
<td><%= cycle.name %></td>
<td><%= cycle.program.try(:name) %></td>
<td><%= cycle.previous_cycle_id %></td>
<td><%= cycle.next_cycle_id %></td>
For cycle.previous_cycle_id
, instead of it printing out 1 for example, I want it to print out the name of the cycle, so Cycle One.
Thanks for the help!
Upvotes: 0
Views: 54
Reputation: 4566
Unless you have the name of the previous cycle stored in the current cycle (or somewhere else in memory), you'll have to query the database to find the name. In your case
<%= Cycle.find(cycle.previous_cycle_id).name %>
Upvotes: 1