How do I sort in descending order on a related resource using Ash.Sort.expr_sort?

I need to sort records based on a field in a related resource, but I am not sure how to indicate that it should be in descending mode.

Here’s the query I am currently using:

   Person
      |> Ash.Query.filter(user_id == ^actor.id)
      |> Ash.Query.load(paper_trail_versions: :user)
      |> Ash.Query.sort(Ash.Sort.expr_sort(paper_trail_versions.id))
      |> Ash.read_first!(authorize?: false, tenant: actor.current_tenant)
  • How can I correctly sort in descending order on a related resource’s field (paper_trail_versions.id) using Ash.Sort.expr_sort?
  • Is there a specific way to reference a related resource in the sort clause of an Ash query?

|> Ash.Query.sort({Ash.Sort.expr_sort(paper_trail_versions.id), :desc})

There is not a syntax for this currently in Ash.Query.sort.

2 Likes

Thanks, this worked.