如何查找具有*所有*匹配类别的项目

How to find items with *all* matching categories

我有两个模型,Item 和 Category,通过连接 table 连接。我想查询 Item 以仅查找与类别列表匹配的项目。我的模型看起来像:

class Item < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

class Category < ActiveRecord::Base
  has_and_belongs_to_many :items
end

我可以轻松找到与任何类别列表匹配的项目。以下将 return 个属于类别 1、2 或 3 的项目。

Item.includes(:categories).where(categories: {id:[1,2,3]})

我只想查找属于所有 3 个类别的项目。使用 ActiveRecord 完成此任务的最佳方法是什么?

我是否需要退回到自己编写 where 条件?如果需要,PostgreSQL 的正确语法是什么?我已经尝试了各种 "WHERE ALL IN (1,2,3)",但只是出现语法错误。

更新:

根据 Find Products matching ALL Categories (Rails 3.1) 的已接受答案,我可以非常接近。

category_ids = [7,10,12,13,52,1162]

Item.joins(:categories).
  where(categories: {id: category_ids}).
  group('items.id').
  having("count(categories_items.category_id) = #{category_ids.size}")

不幸的是,在链接 .count.size 时,我得到的是哈希而不是记录数:

{189 => 6, 3067 => 6, 406 => 6}

我可以计算生成的哈希中的键以获得真实记录数,但这是一个非常不优雅的解决方案。

这段代码怎么样

Item.all.joins(:categories).where(categories: { id: [1, 2, 3] })

SQL 是

SELECT
    "items" . *
FROM
    "items" INNER JOIN "categories_items"
        ON "categories_items" . "item_id" = "items" . "id" INNER JOIN "categories"
        ON "categories" . "id" = "categories_items" . "category_id"
WHERE
    "categories" . "id" IN (
        1
        ,2
        ,3
    )

我不能肯定,但这可能有效

categories = Category.find(1,2,3)
items = Item.includes(:categories)
items.select{|item| (categories-item.categories).blank?}

或者只是

Item.all.select{|item| (Category.find(1,2,3)-item.categories).blank?}

ActiveRecord

对于 ActiveRecord,您可以将这样的方法放入项目中 class:

def self.with_all_categories(category_ids)
  select(:id).distinct.
    joins(:categories).
    where('categories.id' => category_ids).
    group(:id).
    having('count(categories.id) = ?', category_ids.length)
end

然后您可以像这样过滤您的查询:

category_ids = [1,2,3]
Item.where(id: Item.with_all_categories(category_ids))

您还可以使用作用域使其更加友好:

class Item
  scope :with_all_categories, ->(category_ids) { where(id: Item.ids_with_all_categories(category_ids)) }

  def self.ids_with_all_categories(category_ids)
    select(:id).distinct.
      joins(:categories).
      where('categories.id' => category_ids).
      group(:id).
      having('count(categories.id) = ?', category_ids.length)
  end
end

Item.with_all_categories([1,2,3])

两者都会产生这个SQL

SELECT "items".*
FROM "items"
WHERE "items"."id" IN
  (SELECT DISTINCT "items"."id"
   FROM "items"
   INNER JOIN "categories_items" ON "categories_items"."item_id" = "items"."id"
   INNER JOIN "categories" ON "categories"."id" = "categories_items"."category_id"
   WHERE "categories"."id" IN (1, 2, 3)
   GROUP BY "items"."id" 
   HAVING count(categories.id) = 3)

从技术上讲,您不需要该子查询的 distinct 部分,但我不确定使用或不使用是否会提高性能。

SQL

raw 中有几种方法 SQL

SELECT *
FROM items
WHERE items.id IN (
  SELECT item_id
  FROM categories_items
  WHERE category_id IN (1,2,3)
  GROUP BY item_id
  HAVING COUNT(category_id) = 3
)

这将适用于 SQL 服务器 - 语法在 Postgres 中可能略有不同。或者

SELECT *
FROM items
WHERE items.id IN (SELECT item_id FROM categories_items WHERE category_id = 1)
  AND items.id IN (SELECT item_id FROM categories_items WHERE category_id = 2)
  AND items.id IN (SELECT item_id FROM categories_items WHERE category_id = 3)

刚刚尝试了亚历克斯关于 has_many 的惊人建议:通过设置,它产生了一个令人惊讶的结果:当我查找具有完全 [6,7,8] 类别的项目时,它也有 return 个项目匹配所有 6、7、8 个类别以及更多,即。具有 [6,7,8,9] 类别的项目。

根据代码从技术上讲是正确的结果,因为那里的having子句是处理where子句的查询结果,因此Alex的代码中having子句的所有可能计数结果都是1或2或3,但不能超过 4 个。

为了克服这种情况,我添加了一个类别计数器缓存并在 having 子句之前预先筛选了类别计数,因此它只 return 编辑了具有且仅具有 [6,7,8] 类别的项目(没有额外)。

  def self.with_exact_categories(category_ids)    
    self.
      joins(:categories).
      where('categories.id': category_ids).
      where('items.categories_count = ?', category_ids.length).
      group('items.id').
      having('count(categories.id) = ?', category_ids.length)
  end

对于预筛选类别计数,我不知道如何在where子句中使用聚合函数,但仍然很高兴得知计数器缓存在Rails 4.21中仍然有效。这是我的模型设置:

class Item < ActiveRecord::Base
  has_many :categories_items
  has_many :categories, through: :categories_items
end

class CategoriesItem < ActiveRecord::Base
  belongs_to :category
  belongs_to :item, counter_cache: :categories_count
end

class Category < ActiveRecord::Base
  has_many :categories_items, dependent: :destroy
  has_many :items, through: :categories_items, dependent: :destroy
end

class AddCategoriesCountToItems < ActiveRecord::Migration
  def change
    add_column :items, :categories_count, :integer, default: 0
  end
end