mmport80
I have a query like:
Alarms
|> preload(:event)
|> order_by([{:desc, :event[:id]}])
|> Repo.all()
You can see what I am trying to do, reach into the preloaded event and order the alarms by the id field within the event field / association.
I feel I am very close to achieving this, but I also suspect that it is not possible, and I need to use a join.
Any advice?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mmport80
This is the join solution I came up with. Not too elegant..
Unsure how I can generalise this, and reuse it..
peerreynders
It’s not exactly clear what kind of “reuse” (or elegance) you are after.
mmport80
E.g. dropping in a list of things to order by is nice:
versus this:
cannot just drop in these args.. Etc..
peerreynders
It’s easy enough to write a function that produces an “order by” that can vary dynamically.
“Dropping in a list” doesn’t really seem that useful given how tightly that information is coupled to the table schemas.
Are you pursuing a particular usecase?
mmport80
An API with multiple endpoints, where you could create an endpoint and drop in which fields can be / should be ordered / filtered by…
It’s worked well so far, these nested associations are irksome though.
I have found an API in Ecto which looks similar to what I need, just not available for
order_by:from(city in City, preload: :country, select: struct(city, [:country_id, :name, country: [:id, :population]]))Instead you use it to specify fields to return..
peerreynders
So your issue is that you can’t order preloads? Keep in mind that associations are entirely an Ecto concept that has nothing to do with the relational capabilities of SQL and “ORDER BY” is part of the SQL aspect.
mmport80
Good point.
benwilson512
Check out preload queries, they’re perfect for this: Ecto.Query — Ecto v3.14.0
mmport80
Thanks - I looked at those.
I understand preload queries can order the data within the association…
But I am looking to order the whole result set, depending on what you find in the ‘belongs_to’ association.
benwilson512
Ah. You can tweak what you have a little I think and do:
In any case, doing the join so that you can order seems like exactly the right way to do the SQL.