travisf

travisf

If you have a virtual field, say field(:users, {:array, :map}, virtual: true} what is the best way to populate that field with multiple records when querying the record? For example:

defmodule App.Topic do 
  schema "topics" do 
    field(:name, :string)
    field(:description, :string)
    field(:users, {:array, :map}, virtual: true)
  end 
end 

defmodule App.VirtualUser do 
   embedded_schema do 
      field(:name, :string)
      field(:id, :string)
   end 
end 

In this case there is a bit of info from the user that is required by the topic but it may not make sense to load a full User struct for various reasons.

When querying a Topic I’d write something like this:

Topic 
|> from(as: :topic)
|> join(:left, [topic: t], u in assoc(t, :users), as: :user)
|> join(:left, [user: u], p in. assoc(p, :profile), as: :profile)
|> select_merge([topic: t, user: u, profile: p], 
   %{users: %VirtualUser{name: p.name, id: u.id}
)

Perhaps you can see the issue, this works when there is a single user, but when multiple users are associated with a Topic it breaks because select_merge/2 (I think), throws an error: ** (Ecto.MultipleResultsError) expected at most one result but got 2 in query which fair enough.

I’m by no means a Postgres guru or SQL in general for that matter, so what I’m attempting may not be possible but if theres a way of loading/associating multiple records on the record I’m very interested.


Further info: in this case preloads aren’t an option because my ultimate goal is to combine a few different types of records into the VirtualUser struct. The idea being that VirtualUser can be a uniform shape that I can load different records into (for example a User struct and a Guest struct could look very different from each other but each be merged into a VirtualUser struct.

Showing Posts 1 to 2

travisf

travisf OP

I’m not sure I see the advantage of a super clever query here. If somebody has a suggestion I’m all ears but in the interim I ended up just preloading the required associations and then passing them to a function that adds VirtualUser unto the Topic struct:

Topic 
|> from(as: :topic)
|> preload([users: :profile])
|> Repo.all()
|> decorate_topics()

...

def decorate_topics(topics), do:  Enum.map(topics, &decorate_topic(&1))

def decorate_topic(%{users: users} = topic) do 
  virtual_users = Enum.map(users, & %VirtualUser{name: &1.name, id: &1.id})
  
 %{topic | users: virtual_users}
end 

That’s the basic gist of it. In some ways I think this might even be more performant than an elaborate query.

sodapopcan

sodapopcan

Sounds like you’re getting Domain Driven Designy! I would say a new table-based schema for VirtualUser (as opposed to embedded) which in this case I would call Author. It could define only the fields you care about from the users table (and you could potentially remove author-specific fields form the User schema!). However, it sounds like you have more than one type of user table, or plan to? If this is the case then you would need to create a table view to combine them, so this is something you have to decide is worth it. I personally have no problem creating views while others may advice against it, so YMMV. In any event, doing so would allow you to preload and generally do regular query-type things in a standard way with a named domain concept (“authors”)!

— All posts loaded —

Where Next? Top

Trending in Questions Top

Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
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
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
mandar1045
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
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
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
saleyn
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

Other Trending Topics Top

GenericJam
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...
261 13183 112
New
mudasobwa
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
OndrejValenta
(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
kip
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews