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’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Other Trending Topics
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
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










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.