Я пытаюсь сделать что-то очень похожее на этот вопрос
У меня есть 4 модели, одна из которых (CoffeeBlend) является таблицей соединений:
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
таблице есть столбец с именем country_id
, в котором указан идентификатор из таблицы countries
.
В моем coffee_roasts_show
я хочу иметь возможность вывести связанную страну изкофейное зерно.Моя последняя попытка выглядит как
<% @coffee_roast.coffee_beans.country.country_name %>
, которая дает undefined method 'country'
Или
<% @coffee_roast.coffee_beans.countries.country_name %>
возвращает undefined method 'countries'
Есть ли у меня ассоциацииправильный?Мой код шоу неправильный?