Ash FrameworkAF
Ash Framework6mo ago
12 replies
absowoot

Query load and sort

I have a table locations with a geometry field :geom (using ash_geo + geo_postgis). When a user lands on a page and geolocates, the list of locations should sort ASC based on the user location and show the distance away in miles.

My current query is:
query
|> Ash.Query.load(distance_miles: %{geolocation: geolocation})
|> Ash.Query.sort(distance_miles: {%{geolocation: geolocation}, :asc})

and my calculation is:
calculate :distance_miles, :float do
  argument :geolocation, :geometry

  calculation expr(^st_distance_in_meters(geom, ^arg(:geolocation)) / 1609.34)
end


The calculation works as expected but sorting does not (I believe it was working in a previous version). What am I missing and/or can I just calculate distance_miles and just sort on that? IE:
query
|> Ash.Query.load(distance_miles: %{geolocation: geolocation})
|> Ash.Query.sort(:distance_miles)

(I tried this but it also did not work).
Solution
Aha I found the issue was a Enum.reduce flipping the order later on
Was this page helpful?