Hey there, I have a calculation that I implemented the expression/2
callback:
defmodule Core.Pacman.Markets.Entity.Calculations.Purchases do
@moduledoc false
alias Core.Pacman.Markets.Entity.Calculations.Helper
use Ash.Resource.Calculation
import Core.Ash.Macros.ExprMacros
@impl true
def expression(_opts, context) do
%{arguments: %{builder: builder}} = context
distance = Helper.distance(builder)
geo_point = builder |> Helper.geo_point() |> maybe_geo_point(distance)
{start_date, end_date} = Helper.buy_interval(builder)
strategies = Helper.strategies(builder)
{min_beds, max_beds} = Helper.beds(builder)
{min_baths, max_baths} = Helper.baths(builder)
{min_sqrt, max_sqrt} = Helper.building_area(builder)
{min_price, max_price} = Helper.buy_price(builder)
zip_code = builder |> Helper.zip_code() |> maybe_filter_by_zip_code()
financing_type = builder |> Helper.financing_type() |> maybe_filter_by_financing_type()
filters =
[
expr(strategy in ^strategies),
expr_between(:buy_date, start_date, end_date),
expr_between(:buy_price, min_price, max_price),
expr_between(:property_bedrooms, min_beds, max_beds),
expr_between(:property_building_area, min_sqrt, max_sqrt),
expr_between(:property_bathrooms, min_baths, max_baths)
] ++
financing_type ++
geo_point ++
zip_code
expr(count(:transactions, query: [filter: ^filters]))
end
defp maybe_filter_by_financing_type([]), do: []
defp maybe_filter_by_financing_type(types), do: [expr(financing_type in ^types)]
defp maybe_filter_by_zip_code(nil), do: []
defp maybe_filter_by_zip_code(zip_code), do: [expr(property_zip == ^zip_code)]
defp maybe_geo_point(nil, _), do: []
defp maybe_geo_point(_, nil), do: []
defp maybe_geo_point(geo_point, distance),
do: [expr_geo_within(:property_geography, ^geo_point, ^distance)]
end
But when I tried to load it like this: Ash.load!(entities, {:purchases, args}, actor: actor)
, I get the following error:
[error] Task #PID<0.5980.0> started from #PID<0.5964.0> terminating
** (Ash.Error.Unknown)
Bread Crumbs:
> Exception raised in: Core.Pacman.Markets.Entity.read
Unknown Error
* ** (UndefinedFunctionError) function Core.Pacman.Markets.Entity.Calculations.TotalProfit.calculate/3 is undefined or private
(core 1.225.0) Core.Pacman.Markets.Entity.Calculations.TotalProfit.calculate/3
(core 1.225.0) Core.Pacman.Markets.Entity.Calculations.TotalProfit.calculate(...)
(ash 3.5.36) lib/ash/actions/read/calculations.ex:601: Ash.Actions.Read.Calculations.with_trace/4
(ash 3.5.36) lib/ash/actions/read/calculations.ex:540: Ash.Actions.Read.Calculations.run_calculate/7
(ash 3.5.36) lib/ash/actions/read/calculations.ex:520: Ash.Actions.Read.Calculations.run_calculation/3
(ash 3.5.36) lib/ash/actions/read/calculations.ex:406: anonymous fn/3 in Ash.Actions.Read.Calculations.do_run_calcs/4
(ash 3.5.36) lib/ash/actions/read/calculations.ex:401: Ash.Actions.Read.Calculations.do_run_calcs/4
(ash 3.5.36) lib/ash/actions/read/calculations.ex:356: Ash.Actions.Read.Calculations.do_run_calculations/5
(ash 3.5.36) lib/ash/actions/read/read.ex:453: Ash.Actions.Read.do_run/3
(ash 3.5.36) lib/ash/actions/read/read.ex:86: anonymous fn/3 in Ash.Actions.Read.run/3
(ash 3.5.36) lib/ash/actions/read/read.ex:85: Ash.Actions.Read.run/3
(ash 3.5.36) lib/ash.ex:2517: Ash.load/3
(ash 3.5.36) lib/ash.ex:2386: Ash.load!/3
I’m not sure why it is calling calculate/3
instead of expression/2