Nested association, conditional preload

@joey_the_snake Did select get you the data you need? To be clear, I was saying to replace the preload attribute with select as in:

preload_query =
  from(student in Student,
    left_join: college in College,
    on: student.college_id == college.id,
    left_join: course in Course,
    on: college.id == course.college_id and student.course_name == course.name,
    select: %{
      student: student.name,
      college: college.name,
      course: course.name
    }
  )

from(
  state in State,
  preload: [students: ^preload_query]
)
|> Repo.all()

You can choose whatever data you need in select and the query should return State structs with a students array with those custom data structs. Sometimes it is just easier to select what you want rather than relying on preload.

1 Like