I want to calculate the sub_total for a sale (sum of line_items.line_total) without querying the data layer. how can I achieve that?
Here’s my current setup for the sub_total calculation in the Sale resource:
calculations do
calculate :sub_total, :decimal, expr(sum(line_items.line_total)) do
description "Sub total amount for the sale before discounts or taxes"
public? true
end
end`
I’d like to calculate the sub_total using Ash.calculate!/3 by passing line_items data in-memory, like this:
sale_attrs = %{line_items: [%{line_total: 12}, %{line_total: 15}]}
Ash.calculate!(Sale, :sub_total, refs: sale_attrs)`
For context, I’m already doing something similar for line_total in the SaleLineItem resource, which works without hitting the data layer:
calculations do
calculate :line_total, :decimal, expr(quantity * price) do
public? true
description "Total amount for the line item"
end
end`
# I can compute line_total like this:
line_item_attrs = %{quantity: 2, price: 10}
Ash.calculate!(SaleLineItem, :line_total, refs: line_item_attrs)`
How can I configure the sub_total calculation to work with in-memory data (sale_attrs) and avoid hitting the data layer, similar to my line_total calculation?






















