# Preloading a join inside a from function

**URL:** <https://forum.elixirforum.com/t/preloading-a-join-inside-a-from-function/37606>\
**Category:** Questions\
**Tags:** ecto\
**Created:** [February 17, 2021, 8:38am UTC](https://forum.elixirforum.com/t/preloading-a-join-inside-a-from-function/37606 "2021-02-17T08:38:01Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![tjdam](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tjdam/32/20256_2.png) [@tjdam](https://forum.elixirforum.com/u/tjdam)\
**Post date:** [February 17, 2021, 9:39am UTC](https://forum.elixirforum.com/t/preloading-a-join-inside-a-from-function/37606/3 "2021-02-17T09:39:50Z")

</div>

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 `from` function because I wanted to create a custom map using `select` in 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:

```elixir
from u in User, 
  join: r in assoc(u, :user_group_relationships), 
  on: not is_nil(r.role),
  join: g in assoc(r, :group),
  where: g.id == ^id,
  preload: [...],
  select: %{name: u.name, role: r.role}

```

I get:

```elixir
    ** (Ecto.QueryError) the binding used in `from` must be selected in `select` when using `preload` in query

```

I don’t quite understand what it means…

I know the preload works cause if I just use `select: u` it returns the list of `User` structs 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`?

---

_[View the full topic](https://forum.elixirforum.com/t/preloading-a-join-inside-a-from-function/37606)._
