How can I sum all the expenses.amount across all categories

I have a Category resource inside a Wallet domain. The Category has an attribute monthly_budget of type :money. Each category has many expenses, and each expense has an amount (also :money). On a domain level (in Wallet): How can I sum the monthly_budget of all categories? How can I sum all the expenses.amount across all categories? Should I define an aggregate on the resource, or is there a recommended Ash way to sum :money attributes across multiple records? Thank you in advance
2 Replies
ZachDaniel
ZachDaniel2mo ago
Have you read the docs on aggregates? nvm, this isn't for relationships Ash.sum(Category, :monthly_budget) You could then add an aggregate on category:
aggregates do
sum :total_expenses, ...
end
aggregates do
sum :total_expenses, ...
end
and then you could do Ash.sum(Category, :total_expenses) With that said, you can use Ecto with Ash resources to do arbitrary SQL stuff
nagieeb
nagieebOP2mo ago
Amazing , thank you I rely on the ash framework book and reading documentation too , but thought it would be in the aggregate and calculations stuff Thank you very much

Did you find this page helpful?