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
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










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.