Getting sorted relations from resource via code interface

All,

I’m currently struggling to sort relationships loaded from a resource.

E.g. a resource “Domain” has multiple “Controls”.

Getting a domain and preload the controls is easy and works just fine:

Domain.get_by_identifier!(identifier, load: :controls)

However I’m struggling with getting those controls as sorted list. My gut feeling is that this should be possible because the Api.load/3 function also accepts [atom() | {atom(), atom() | Keyword.t()}] as load_statement type. This would allow for something like

Domain.get_by_identifier!(identifier, load: [{:controls, [identifier: asc]}])

This doesn’t work though :frowning:

Any pointers?

CU,

Udo

This is done via passing a query as the value :smiley:

Domain.get_by_identifier!(identifier, load: [controls: Ash.Query.sort(Control, identifier: :asc)]_

Thanks for the answer! Works perfectly!

I assume I can do any Ash.Query magic using this then, correct?

This might blur the line of idiomatic actions … but definetly a nice trick to have in the bag.

Thanks,

Udo

For the most part, yes. Ultimately an action gets called on the destination resource that validates/authorizes whatever you’re doing. If you want to put it behind an action you can do it like this:

read :... do
  prepare fn query, _ -> 
    Ash.Query.load(....)
  end
end
1 Like