makeitrein
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…
Trending in Chat/Questions
Other Trending 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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
josevalim
You should be able to use the
dynamicmacro to build all of the OR expressions and then interpolate it into the query at once: Ecto.Query — Ecto v3.14.0makeitrein
Thx for the pointer Jose, ended up with something not so different from my original implementation: