Reputation: 61
In my application I have 2 models: sushi(table 1) and type_sushi(table 2) with foreign key in between. Everthing is running like I expected but when I am showing the table 1 data, it shows me the sushi type like your id (1, 2, 3) but I'd like to show the name of sushis type like this:
I'd like to show like the "Tipo sushi" like the name of the type (yaksoba, sashimi for exemple) and not the id (1, 4 for exemple).
Could you guys help me?
<% @sushis.each do |sushi| %>
<%= sushi.name %>
<%= sushi.price %>
<%= sushi.tipo_sushi_id %> -> (Here is I need to change something I guess)
<%= link_to 'Show', sushi %>
<%= link_to 'Edit', edit_sushi_path(sushi) %>
<%= link_to 'Destroy', sushi, method: :delete, dat`enter code here`a: { confirm: 'Are you sure?' } %>
<% end %>
Update: I've updated "sushi.tipo_sushi_id" to "sushi.tipo_sushi.name" but I've got this message: undefined method `arel_table' for TipoSushi:Module.
Upvotes: 2
Views: 2145
Reputation: 61
I solved the problem by add in model/sushi.rb, the comand belongs_to :tipo_sushi, class_name: SushiTipo
.
Thank you guys!
Upvotes: 3
Reputation: 239312
Your code is explicitly written to show the ID. If you want to show the name, this...
<%= sushi.tipo_sushi_id %>
Is obviously not going to do it.
Instead, you need to load the related record and display its name:
<%= sushi.tipo_sushi.name %>
This assumes you've set up your associations correctly.
Upvotes: 0