imjp
imjp

Reputation: 6695

How do I output name instead of ID?

Rails newbie here! I have a has_many_and_belongs_to_many relationship between items and builds.

In the code provided below, the ID of the items are being displayed instead of the name of the item with that ID. How do I display the name of the item?

<% current_user.builds.each do |build| %>
  <tr>
    <td><%= build.hero.name%></td> 
    <td><%= build.user.email%></td> 
    <td><%= build.name %></td> 
    <td><%= build.starting_items %></td> 
    <td><%= build.early_items %></td> 
    <td><%= build.core_items %></td> 
    <td><%= build.situational_items %></td> 
  </tr>
<% end %>

Upvotes: 0

Views: 103

Answers (1)

Neelesh
Neelesh

Reputation: 1478

If you mapped association (has_one) in model with Hero and User, then probably you can use following code:

build.hero.field_name # Where field_name is column name from Hero Model

Please let me know if this will work for you or not.

Upvotes: 2

Related Questions