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
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
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
Popular in Chat/Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








