arturz

Back

Rivers fork

Sometimes you may want to pass a custom query to preload function.

There’re department with employees schemata that can be soft-deleted:

Let’s say we want to get department info with all employees, including those that were deleted.

❌️ We cannot just preload it, because has_many field has where: [deleted_at: nil] filtering association.

HR.Department
|> preload([_department], :employees)
elixir

❌️ You can pass custom query to preload via joined binding, but it won’t work if you use the assoc macro which respects filtering associations.

HR.Department
|> join(:left, [department], employees in assoc(department, :employees))
|> preload([_department], {employees, :employees})
elixir

✅ Instead, you need to build your joined binding from scratch:

HR.Department
|> join(:left, [department], employees in HR.Employee, on: department.id == employee.department_id)
|> preload([_department], {employees, :employees})
elixir