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: https://hexdocs.pm/ecto/Ecto.Query.html#dynamic/2

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

meraj_enigma
Hey, What’s a good resource to learn Microservices in Elixir/Phoenix? Is there any book/docs/Github repos that I can refer to? Thanks, -M
New
xgilarb
Hi there, I’m interested in using Elixir because of the rumors about the reliability of the Phoenix framework, and surprisingly, Elixir’...
New
shansiddiqui94
Hello all, I recently did my first app in Phoenix and Liveview, many thanks to all the users who assisted me. I found that the tutorial ...
New
ericdouglas
I think that would be really interesting to have official books created by the community about all kinds of development we can do with El...
New
stevensonmt
I’d like to provide my review of the Elixir Course module from Groxio. I have some criticisms but I’d like to start with the positives. ...
New
jace
I wanted to write a library for interacting with a Web API as a practical way of learning Elixir. However, there seem to be a lot of diff...
New
pdgonzalez872
Do we have a list of academic/research papers: about Elixir/Erlang? that use Elixir/Erlang? about the Beam? If so, could you please po...
New
younes-alouani
I'm studying Phoenix Framework but I want to understand basics first. Which book/mooc explains better the Design of Network-based Softwar...
New
logesh
Could someone provide a learning path for functional programming for who came from oops background.? Thanks in advance
New
shansiddiqui94
Greetings Elixir Developers, My name is Daniel, and I am taking my first step to learn all about the Elixir language. Currently I have a...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
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 record...
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
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

We're in Beta

About us Mission Statement