makeitrein

makeitrein

Ecto - creating a dynamic where OR clause

More Ecto questions! More madness!

The context: there’s a list of books that I want to filter with a dropdown…

The dropdown: looks something like (gt stands for greater than, lt stands for land tanks)…
def word_count_options do [ "0 - 5k": [gt: 0, lt: 5000], "5k - 30k": [gt: 5000, lt: 30000], "30k - 100k": [gt: 30000, lt: 100000], "100k - 200k": [gt: 100000, lt: 200000], "200k+": [gt: 200000, lt: 1000000000] ] end

The current code: almost works, but I would like to make this into ONE where clause with OR between the word count checks (so I can compose other queries that use where with this query without mucking up my intended filter logic)…

Enum.reduce(filters, query, fn filter, query -> 
options = Fic.word_count_options[String.to_existing_atom filter] 
from f in query, or_where: f.word_count > ^options[:gt] and f.word_count < ^options[:lt] 
end)

What’s the best path forward here? The “dynamic” keyword kinda seem like what I need, but I feel there’s a more straightforward way to handle this pattern…

Most Liked

josevalim

josevalim

Creator of Elixir

You should be able to use the dynamic macro to build all of the OR expressions and then interpolate it into the query at once: Ecto.Query — Ecto v3.14.0

makeitrein

makeitrein

Thx for the pointer Jose, ended up with something not so different from my original implementation:

dynamic = Enum.reduce(filters, false, fn filter, query -> 
    options = Fic.word_count_options[String.to_existing_atom filter] 
    dynamic([f], f.word_count > ^options[:gt] and f.word_count < ^options[:lt] or ^query)
end)

from f in query, where: ^dynamic

Where Next?

Popular in Chat/Questions Top

Besto
I’ve been trying to start learning Elixir for a couple of weeks while I develop a tiny project I have on node.js, but every time I try to...
New
jsnoble
I come from a javascript background and I just starting learning elixir. I went through the basic tutorials but I want something deeper. ...
New
Fl4m3Ph03n1x
Background I have been reading quite a lot about design and architecture in Elixir and overall FP. My latest incursions took to me the On...
New
Emily
Preface: I’m not sure if thise is the right place, because this is not direclty Elixir related… but I’ve always got some of the best advi...
New
roshan
Hi everyone, I’m looking for a book on Phoenix server hosting / deployment like the following books for Rails, Docker for Rails Develop...
New
wolfiton
Question: Can someone recommend me some good resources on learning performance for phoenix elixir applications and a design pattern I sh...
New
satoru
I’m working on the “Bob” exercise on the Elixir Track in Exercism. I am testing for uppercase letter with this simple check: c in ?A..?Z...
New
venomnert
Background I have been a backend elixir developer for about 3 years now. I have been mainly working on simple CRUD applications. Context...
New
Yoga
Or at least, in the works? All I can find are bits and pieces but not a single project from start to finish. Including things like i18n,...
New
prxssh
I’ve been writing Elixir and Phoenix professionally for the past year and have grown quite fond of them. I’ve read books like Elixir in A...
New

Other popular topics Top

johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

We're in Beta

About us Mission Statement