diff --git a/lib/ash/actions/sort.ex b/lib/ash/actions/sort.ex index db6f8a15..b48e5878 100644 --- a/lib/ash/actions/sort.ex +++ b/lib/ash/actions/sort.ex @@ -34,6 +34,7 @@ defmodule Ash.Actions.Sort do calc -> {module, opts} = calc.calculation + Code.ensure_compiled(module) if :erlang.function_exported(module, :expression, 2) do if Ash.DataLayer.data_layer_can?(resource, :expression_calculation_sort) do diff --git a/lib/ash/query/aggregate.ex b/lib/ash/query/aggregate.ex index 0dd5fbd6..33dddd95 100644 --- a/lib/ash/query/aggregate.ex +++ b/lib/ash/query/aggregate.ex @@ -141,8 +141,18 @@ defmodule Ash.Query.Aggregate do List.first(relationship_path) ) + path_for_checking = + relationship_path + |> tl() + |> Enum.reduce({[], aggregate_resource}, fn rel, {path, resource} -> + relationship = Ash.Resource.Info.relationship(resource, rel) + {[relationship | path], relationship.destination} + end) + |> elem(0) + |> Enum.reverse() + {in_query?, reverse_relationship} = - case Load.reverse_relationship_path(relationship, tl(relationship_path)) do + case Load.reverse_relationship_path(relationship, path_for_checking) do :error -> {ref_path == [] && can_be_in_query?, nil}