magosi

magosi

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

Hi. In my application I have a many to many association between Product and Tag schemas which goes through the ProductTag table. Quite straight forward, however, I need to list all products that have ALL the tags in a given list. I really cannot find any answer as to how one can do this. This is what I have so far:

def list_category_products(categories) do
        from(
            p in Product,
            join: pt in ProductTag, on: pt.product_id == p.id,
            join: t in Tag, on: pt.tag_id == t.id,
            where: t.name in ^categories
        )
        |> Repo.all()
    end

Lets assume I have product1 associated with the tag “cake” and product2 associated with the tag “tea”. If I pass only “cake” or only “tea” to the function, I get the correct product, but if I pass both “cake” and “tea”, I get both products where I expect to receive none as none of my products have both tags.

I will appreciate any help or advice. Thank you all in advance.

Most Liked

fuelen

fuelen

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

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement