tjdam
Hi!
I have this somewhat convoluted query (for me at least) that fetches an User based on a given Group id he’s part of and if it has an assigned role in this Group. Like so:
from(g in Group,
where: g.id == ^g.id
inner_join: r in assoc(g, :user_group_relationships),
on: not is_nil(r.role),
inner_join: u in assoc(r, :user),
select: u)
Then I end up with this list of Users by running the query through Repo.all, and call Repo.preload to preload a field in User schema.
I do that because I couldn’t figure out how to include this preload bit in that from function before.
I have two questions;
-
Is doing that (running a query with
Repo.alland then preloading) inefficient or does Ecto merge everything in one operation behind the scenes anyways? -
How to preload an
Userfield in this case?
Thanks!
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
No it does not. But doing two queries in not by by default.
It’s likely simpler to reverse the query:
tjdam
Oh my god, hahaha
Yeah, I guess that makes A LITTLE (a lot) more sense, thank you!
But the thing is, I was trying to bring the preload to the
fromfunction because I wanted to create a custom map usingselectin the end of the query, and I thought I wouldn’t be able to preload it afterwards if the result of the query would not be a Ecto struct.But if I go:
I get:
I don’t quite understand what it means…
I know the preload works cause if I just use
select: uit returns the list ofUserstructs fine and if I select a tuple (i.e.select: {u, r.role}) I get the results I want, but then I have to map the result to create the new map for each element returned, which doesn’t seem right to me.Is there a way to do that new map straight from
select?tjdam
I realized it means I wasn’t including the preload in the select… obviously.
I don’t know if its unproductive to keep writing in this thread since my issues seem to be slightly moving away from the original question, but the issue I’m having now is that by using
select: %{name: u.name, preloaded_field: u.preloaded_field}I get an error saying the field I’m preloading is a “virtual field”.Isn’t that always the case for preloads, though?
My reasoning is that because preload happens after the query I can’t use the result of a preload to form the result of my query, is that right?
In that case, I’d always have to query, preload and then map the results get a final result?