Исходя из этого вопроса
class CoffeeRoast < ApplicationRecord
has_many :coffee_blends
has_many :coffee_beans, through: :coffee_blends
has_one :country, through: :coffee_beans
end
class CoffeeBean < ApplicationRecord
has_many :coffee_blends
has_many :coffee_roasts, through: :coffee_blends
belongs_to :country
end
class Country < ApplicationRecord
has_many :coffee_beans
end
class CoffeeBlend < ApplicationRecord
belongs_to :coffee_bean
belongs_to :coffee_roast
end
Я могу показать связанные coffee_beans
на странице показа coffee_roasts
, а также country
, однако,Я не могу понять, как правильно их отображать.
Мой текущий код пытается показать бин и связанную с ним страну в одной строке таблицы, однако верхняя часть пуста, и обе страны находятся ввторой ряд.
coffee_roasts / show.html.erb
<h1 class="display-3"><%= @coffee_roast.name %></h1>
<p>
by <h2 class="display-6"><%= @coffee_roast.roaster.roaster_name %></h2>
</p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Bean</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<% @coffee_roast.coffee_blends.each do |blend| %>
<tr>
<th><%= blend.coffee_bean.name %>
<% end %></th>
<th><%= @coffee_roast.coffee_beans.map {|cb| cb.country.country_name }.join(', ') %></th>
</tr>
</tbody>
</table>
Bean |Страна
El Martillo |
Финка Ла Кумбре |Сальвадор, Гватемала
El Salvador
показывают, что они соответствуют бобу El Martillo
.
Мой подход здесь совершенно неправильный, но я довольно плохо знаком с работой с несколькими уровнямимодели так еще учатся.
Как я могу заставить ассоциированную страну бобов отображаться рядом с бобом?