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

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
332 31312 154
New
Nopp
Hey guys and girls, i am completely “new” to programming, recently played a bit with Python, Ruby and PureBasic, but i want to try somet...
New
Chawki
hi,i’m new to programming world i had learned front-end( javascript,react.js) and i wanna learn a back-end programming language i thought...
New
TwistingTwists
I want to learn DSL. Don’t know how to write one. What;s the best introductory resource? I see some macro being used here. Is DSL only ...
New
lgmfred
Hello, I want to get started with elixir today. Having learnt the basics of Erlang (my first ever language) and seen what it’s can do, I’...
New
marciol
Hey, I have very restricted resources and time so I’m trying to understand the best way to learn Liveview in terms of cost/time. The Pra...
New
armanm
I know zero downtime deployment can mean different things depending on your application and what your users can tolerate so expectations ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement