【发布时间】:2018-12-21 17:19:35
【问题描述】:
我正在尝试做一些与question非常相似的事情
我有 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 表中的ID。
在我的coffee_roasts_show我希望能够拉出咖啡豆的关联国家。我最近的尝试看起来像
<% @coffee_roast.coffee_beans.country.country_name %>
这给了undefined method 'country'
或者
<% @coffee_roast.coffee_beans.countries.country_name %>
返回undefined method 'countries'
我的关联是否正确?我的节目代码错了吗?
【问题讨论】:
标签: ruby-on-rails model-associations