Hi,
I’m trying to preload some data to my user and I would like to add number of user notifications to the preload, not the notifications itself, just the count.
query # fetches the user
|> Repo.one()
|> Repo.preload([:profile, notifications: from(n in Notification, group_by: n.id, select: count(n.id))])
With this query I get one entry inside the list for each notification.
#MyApp.Accounts.User<
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: 6,
...
notifications: [1, 1, 1, 1]
How do I count the notifications with the db so I would get:
#MyApp.Accounts.User<
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: 6,
...
notifications: [4]
Thanks