I'm trying to find how many projects belong to a particular user.
I know that you can do this to get the count of a model:
count = User |> Repo.aggregate(:count, :id)
But how can I find the count of projects that belong to that user?
Projects
table belongs to users
table, so it has the foreign key so you can do aggregate count on Project model where project.user_id == ^user_id
import Ecto.Query
def count(user_id: user_id) do
query = from project in Project,
where: project.user_id == ^user_id
Repo.aggregate(query, :sum, :amount)
end