【发布时间】:2017-03-14 10:47:02
【问题描述】:
我拥有的模型:
类别:
class Category < ApplicationRecord
has_many :categorizations
has_many :providers, through: :categorizations
accepts_nested_attributes_for :categorizations
end
提供者:
class Provider < ApplicationRecord
has_many :categorizations
has_many :categories, through: :categorizations
accepts_nested_attributes_for :categorizations
end
分类:
class Categorization < ApplicationRecord
belongs_to :category
belongs_to :provider
has_many :games, dependent: :destroy
accepts_nested_attributes_for :games
end
游戏:
class Game < ApplicationRecord
belongs_to :categorization
end
我需要展示属于特定提供商的游戏。我试着这样做:
<% @provider.categorizations.joins(:games).each do |game| %>
<%= game.title %>
<% end %>
它给了我一个错误:NoMethodError: undefined method 'title' for #<Categorization:0x007f2cf6ee49e8>。所以,它循环通过Categorization。循环连接 games 表的最佳方法是什么?谢谢。
【问题讨论】:
标签: ruby-on-rails join activerecord