Ash FrameworkAF
Ash Framework3y ago
5 replies
Terryble

How to do the LiveComponent preload example in Ash.Query?

In the docs for the Phoenix.LiveComponent (https://hexdocs.pm/phoenix_live_view/Phoenix.LiveComponent.html#module-preloading-and-update) there is a section there that shows how to use preload/1 to solve the N+1 problem:

def preload(list_of_assigns) do
  list_of_ids = Enum.map(list_of_assigns, & &1.id)

  users =
    from(u in User, where: u.id in ^list_of_ids, select: {u.id, u})
    |> Repo.all()
    |> Map.new()

  Enum.map(list_of_assigns, fn assigns ->
    Map.put(assigns, :user, users[assigns.id])
  end)
end


How would I do the query in this section in Ash.Query?
Was this page helpful?