Ecto query many to many associations that match all elements of a list

Hi David,
List of product ids can be retrieved using this query (I didn’t test):

from tags in Tag,
  join: products_tags in ProductTag, on: products_tags.tag_id == tags.id,
  where: tags.name in ^categories,
  select: products_tags.product_id,
  group_by: products_tags.product_id,
  having: count() == ^length(categories)

After that you can get list of products using subquery or just separate query

1 Like