【发布时间】:2020-09-27 16:59:21
【问题描述】:
我有 3 个模型
项目
class Project < ApplicationRecord
has_many :taggings
has_many :tags, through: :taggings
end
标记
class Tagging < ApplicationRecord
belongs_to :tag
belongs_to :project
end
标签
class Tag < ApplicationRecord
has_many :taggings
has_many :projects, through: :taggings
end
简而言之,项目有许多标签彻底的标签。
我想找出具有所有给定标签的项目。我的输入是tag ids 的数组(例如 [1,3,5])。我试过了
Project.joins(:tags).where(tags: {id: [1 ,3, 5]})
但它会找到具有来自[1,3,5] 的任一标签的项目。我正在寻找具有所有输入标签的项目。我该怎么做?
【问题讨论】:
标签: ruby-on-rails postgresql activerecord