How can we get a record by id?
I was able to figure out that i can get one with:
but I would like to get it with:
This doesn't work:
My code:
10 Replies
Maybe you need to uncomment
filter [id: :id]
?filter expr(id == ^arg(:id))
is the idiomatic way to do it
You can read more about that in the expressions guide.
You can also do it without adding arguments and filters to the action, by specifying get_by: [:id]
on the code interface.Thanks Zach, both work. Doesn't
filter expr(id == ^arg(:id))
mean that it will first fetch all records from the database and then filter them to find the id I am looking for?Nope π
Filters happen in the database
awesome
I landed in the second suggestion
If youβre familiar with ecto, we have our own expression syntax that we can put into database queries.
Similar to what ecto can do.
My Elixie exposure isn't big, but I know ecto and used it in some pet apps, but I only do this time to time
Gotcha. Yeah so
expr
is a limited syntax that supports specific things to run in the database (and sometimes not in the database, the framework takes care of that).
You can read more about it in the expressions guideI am thinking in using ASH to build a saas pet project, and my concerns are with queries performance
and not being able to customize stuff as needed
be them queries or other parts
of the logic
I really appreciate that you take the time to answers my questions. Thank you very much π
My pleasure π Ash is pretty optimized in a lot of ways (and I add optimizations regularly), but there are plenty of escape hatches where you can make it do w/e you want, including if you need to optimize something.