So I have 3 tables borrower, avatar and loan
Borrower -> has_one avatar
Borrower -> has_many loan
Loan Controller
def list_borrowers do
query =
from(
p in Borrower,
select: p,
preload: [:avatar],
preload: [:loan] # I WANT TO AGGREGATE THIS USING COUNT
)
IO.inspect(Repo.all(query))end
This is working properly my problem is I don't know how to aggregate the loan.
I just want to know the number of all the loans of that borrower.
I am unaware of any possible way to preload
the aggregate, but you might declare virtual field instead and include it into your query as:
from p in Borrower,
join: l in Loan,
on: p.id == l.borrower_id,
select: %{p | loan_count: count(l.id)}, # virtual field
preload: [:avatar]