script

script

Put conditional clause dynamically in where query

I got a list of maps like this:

 [%{"id" => 1}, %{"name" => "John"}]

I want to insert them in where query. These maps can be of any size.
The end query should be like this:

 From q in table,
 where: q.id == ^1 or q.name == "John"

I cannot hard code the above query due to the dynamic size of the list.

The end goal is to add each map with or inside where. I cannot use or_where due to requirements constraint.

Dynamics don’t solve this for me.

Is there any solution or workaround.

Any help or suggestion will be much appreciated.
Thanks

Marked As Solved

OvermindDL1

OvermindDL1

Yeah this seems precisely the use case of a dynamic:

where = Enum.reduce(conditionals, false, fn {key, value}, acc ->
  dynamic([q], acc or field(q, ^key) == ^value))
end)

from q in table,
where: ^where

Also Liked

LostKobrakai

LostKobrakai

Why though? This is exactly what the dynamic macro is for.

script

script

Thanks I will take a look into dynamics again.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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

We're in Beta

About us Mission Statement