【发布时间】:2018-03-12 16:46:33
【问题描述】:
我想查找只有一组特定标签的产品。
例如:具有两个或多个 id 的数组:[1, 2] 并且只返回具有这两个标签的产品。
我使用过Product.includes(:tags).where('tags.id' => [64, 65]).all,但这会返回包含其中一个这些标签的任何产品。
这是我的模型:
# models/product.rb
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
# models/tag.rb
has_many :taggings
has_many :products, through: :taggings
# models/tagging.rb
belongs_to :product
belongs_to :tag
【问题讨论】:
标签: ruby-on-rails associations rails-activerecord