fireproofsocks
I have once again nose-dived into the treeline while attempting to follow the official docs… I’m trying to stay positive here, but I am really feeling malnourished when it comes to nutritious examples. I would love some help clarifying the mystery presented on Ecto.Query — Ecto v3.14.0
dynamic = false
dynamic =
if params["is_public"] do
dynamic([p], p.is_public or ^dynamic)
else
dynamic
end
dynamic =
if params["allow_reviewers"] do
dynamic([p, a], a.reviewer == true or ^dynamic)
else
dynamic
end
from query, where: ^dynamic
-
First off, how might we actually pass in a value from the parameters into the where condition? The examples conveniently sidesteps that critical use-case. Like what if you want to filter a list of posts by the author? Would that be something like this?
dynamic =
if params[“author”] do
dynamic([p], p.author = params[“author”] or ^dynamic)
else
dynamic
end -
What’s up with the
orandandin these? If I assume all the filtering parameters I provide must be fulfilled, doesn’t that mean that I should always use “and”? What’s up with thisor ^dynamic? Can someone explain how and/or affects this and can someone explain this bizarre syntax? The only toe-hold my brain can get on this at present comes from some old-school query-string concatenation where the select criteria would beWHERE 1– then each subsequent clause could always begin with “AND condition=something”. Is that what’s going on? -
What’s up with the naked
from query, where: ^dynamic? What’s going on there? How can I actually use that in a query? I’m used to seeing something likequery = from p in Post– I don’t understand that at all, really, but at least it’s pervasive throughout the Ecto Query docs. -
How do we use this in a query that involves a join? It seems that it trips over the “where” clause that defines the join?
If I can get some help wrapping my head around this I can put together a PR that provides some examples that are easier to follow. Thanks for any guidance!
Trending in 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
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
There’s
where: …andor_where: …orwhere: (p.published == true or p.preview == true) and p.id > 100Ecto.Query — Ecto v3.14.0
Ecto.Query — Ecto v3.14.0
There are bindingless operations and dynamic does probably also handle it’s own “binding”.
Ecto.Query — Ecto v3.14.0
joindoes not usewhere: …, buton:to determine the join condition.wherein dynamic does work on joined resources just like without dynamic:or
The
a.id == c.article_idpart could by replaced with a “dynamic”.I’m aware that ecto queries are complex, especially as they support two syntaxes, but all of the things you asked about seem to be already documented in that module. I can certainly understand that this example might not be optimal, but you should keep in mind that those are a ongoing effort and especially as someone knowing the system it’s sometimes hard to anticipate the difficulties someone less knowledgeable might have reading any of those examples.
fireproofsocks
Thank you, this is vastly more useful than what is currently in the docs.
The
joinexamples on the Query page frequently usewhere… there must be cases when it is equivalent toon?The “or” that was (is) specifically bothering me is in here:
dynamic([p], p.is_public or ^dynamic)– why are we interpolating thedynamicvariable again? Especially into a condition that will always be false?LostKobrakai
whereis like in SQL justwhere. Theonpart in joins can be skipped forjoin: c in assoc(a, :comments), because by using the association between schemas ecto does already know how to join things.I doubt the
falsebeing a condition in that example, but rather a “blank slate”. If you join…, where: ^dynamicanddynamic = falsethen it’s probably just not adding a where clause at all.dynamic([p], p.is_public or ^dynamic)this is then just “take the prev. dynamic and add onto it”. Though to be honest I’ve not worked that much with dynamic by now and might be wrong on on that onefireproofsocks
Interesting. Thanks for the further clarifications! I’ve got it working now, it just ends up being pretty long.
Have you ever tried to use a loop to define all filterable columns? Something like this:
Although that doesn’t work because of the
a.columnisn’t interpreted as a variable, but that was the idea. Any suggestions? Thanks!LostKobrakai
Enum.reducecan do that quite nicely (you need to aggregate the dynamic not overwrite it on each iteration).fireproofsocks
Nice. Ok, I see how to use the
field()function now, thanks! Is there any way to dynamically set the operator? For flexibility, it would be really useful for CRUD searches and the like to be able to have a function that would allow the user to specify which operator to use for each field – the default would be=, but it would be cool to dynamically specifygtfor>or trigger aLIKEmatch of some sort…benwilson512
I’m not entirely sure what the point of commentary like this is. The Elixir community is still relatively small, and the docs and code that exist are almost always the work of a limited number of people doing the best they can with the time they have available.
That isn’t to say that they’re perfect or even close. I’m confident that you’re right, that the docs could use more examples. Nonetheless, as the recipient of code, docs (even if flawed), personalized help, starting a post with a complaint just depresses those you’re seeking help from.
fireproofsocks
Sorry, you’re right: there’s nothing positive in commentary like that. I apologize. I was writing from a point of extreme frustration and exhaustion. I would like to submit some examples once I can get things working.
benwilson512
We’ve definitely all been there, and I understand. Thanks for the apology, and I’m sure that the docs will benefit greatly from your examples!
peerreynders
Though frankly:
Ecto queries are composable.
Ecto.Query.dynamic/2comes in handy when you need yourwhereconditions to be composable.Conditions are composed with
andoror:orand you don’t need the condition, you default tofalseinstead.andand you don’t need the condition, you default totrueinstead.Furthermore if you are simply
anding the conditions you need, you probably don’t needEcto.Query.dynamic/2:For relatively simple condition composition (more like “chaining” really) with
orEcto.Query.or_where/3can be used in exactly the same manner.dynamic/2becomes necessary when you are composing deeply nested, optional conditions