【发布时间】:2015-05-20 04:05:16
【问题描述】:
- 导轨 4.2
- 红宝石 2.1
我有两个非常基本的模型(产品和标签),通过另一个模型(标签)与 has_many 关联。
我有另一个模型(类别)与上述模型(产品)具有一对多连接。
问题:
如何在视图中显示具有特定产品类别的产品的标签列表?
换句话说:是否可以列出来自特定产品类别的所有标签?
型号:
class Product < ActiveRecord::Base
has_many :taggings
has_many :tags, through: :taggings
belongs_to :category, counter_cache: true
end
class Tag < ActiveRecord::Base
has_many :taggings
has_many :products, through: :taggings
end
class Tagging < ActiveRecord::Base
belongs_to :product
belongs_to :tag, counter_cache: :products_count
end
class Category < ActiveRecord::Base
has_many :products
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 model associations