Which module define the `assoc` in a query?

When reading the doc of Ecto.Query.join, I found a piece of code:

from p in Post,
  left_join: c in assoc(p, :comments),
  select: {p, c}

I am wondering which module define the assoc above. I have checked:

  • Ecto.Query - failed to find it.
  • Ecto.Query.API - failed to find it.
  • Ecto.Query.Build - failed to find it.
  • Ecto.assoc - apparently, I think, Ecto.assoc is not what I am finding.

Can anyone give some tip on this? Thanks in advanced. :wink:

1 Like

I guess assoc should be in the Ecto.Query.API module, but it’s not, so this should be a documentation issue.

Actually, there’s no such function in Ecto. So how can a non-existing function be called? If you noticed that Ecto.Query.join is a macro, you will understand. You can fire up an iex session without loading any deps, and try this:

iex> quote do: assoc(p, :comments)
{:assoc, [], [{:p, [], Elixir}, :comments]}

The expression assoc(p, :comments) is halfway compiled to Elixir AST, then dissected and reassembled into something else, rather than executed at run-time.

This is where the magic happens (line 75):