So I’m back with more Ecto questions.
I have a rather large query with many joins and preloads. I would like to split this up into smaller “query” functions I can reuse across multiple functions.
From the Ecto.Query
docs describe how one can compose queries which join tables like so:
def load_joined_table(query) do
query = from rows in query,
left_join: other_table in assoc(rows, :other_table)
query
|> join_nested_table
|> join_other_nested_table
end
def join_nested_table(query) do
from [rows, other_table] in query,
left_join: nested_table in assoc(other_table, :nested_table)
end
def join_other_nested_table(query) do
from [rows, other_table] in query,
left_join: other_nested_table in assoc(other_table, :other_nested_table)
end
It does however not mention how to preload these nested associations.
How can one do that?
I’ve tried the following:
def load_joined_table(query) do
query = from rows in query,
left_join: other_table in assoc(rows, :other_table),
preload: [other_table: other_table]
query
|> join_nested_table
|> join_other_nested_table
end
def join_nested_table(query) do
from [rows, other_table] in query,
left_join: nested_table in assoc(other_table, :nested_table),
preload: [other_table: {other_table, nested_table: nested_table}]
end
def join_other_nested_table(query) do
from [rows, other_table] in query,
left_join: other_nested_table in assoc(other_table, :other_nested_table),
preload: [other_table: {other_table, other_nested_table: other_nested_table}]
end
But this does not seem to work for me. So how does one split up preloads into separate composable functions? Am I missing something obvious (I often do )