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

asfand
I am Asfandyar from Pakistan. This is my first time to the forum. I develop PHP websites using CodeIgniter, which is super easy to learn...
New
markdev
What are the best beginner resources for learning Elixir and OTP (not Phoenix) in 2018?
New
Nopp
Hello there, i have a lot to read and to learn, but are there some books, that are focussing on how i “should” plan the architecture of ...
New
zervis
Hello, I’m about to dive into web development. I was thinking about Laravel or Ruby on Rails, but then I found Phoenix. Do you recommen...
New
pietrofxq
I’ve bought the following books: Programming Elixir 1.6 Programming Phoenix 1.4 Programming Ecto Functional Web Development with Elixir...
New
QueenSvetlana
My bookstore has Introducing Elixir: Getting Started In Functional Programming and the front cover says the book uses Elixir 1.4. The cur...
New
loganhelms
A while back, I read a great book by Luis Atencio titled, Functional Programming in JavaScript. In section 7.3, he discusses memoization ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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

We're in Beta

About us Mission Statement