Owens
Preload only first nested association's association
Hello all,
I’m doing a DB call for the index page of a WhatsApp-style chat app.
I have ChatsUsers → Chats → Messages.
I need to get the list of the user’s chats ordered by recency of the last_message, and just for the first Chat preload all of its Messages.
See below for reference.
![]()
Here’s the DB call I’m trying to make
ChatUser
|> where(user_id: ^user_id)
|> join(:left, [cu], c in assoc(cu, :chat))
|> order_by([cu, c], [{:desc, c.last_message}])
|> preload([cu, c], chat: c)
|> Repo.all()
This will order all the ChatsUsers based on the descending order of its chat.last_message.
But now I need to preload the messages to only the first ChatsUsers’s chat.
Any tips appreciated!
Most Liked
LostKobrakai
Why “preload” the messages in the first place? I’d load load those messages separately (in liveview components are great for that).
1
fuelen
Hello!
For my use case I used this:
threads # list of structs
|> MyApp.Repo.preload(messages: {MyApp.Chat.recent_messages_in_thread(), [:author]})
defmodule MyApp.Chat do
def recent_messages_in_thread(query \\ MyApp.Chat.Message) do
from messages in query,
distinct: messages.thread_id,
order_by: [asc: messages.thread_id, desc: messages.inserted_at]
end
end
I hope the idea is clear
1
fuelen
Popular in Questions
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Other popular topics
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









