Error when using Ash.load on a relationship called "resource"

I have an ash resource called “Resource” and it is causing issues when I try to use Ash.load since the function seems to expect a keyword called :resource which is conflicting with mine.

It hasn’t been a problem when simply specifying it, like prepare build(load: [resource: [:tenant, user_tenant: [:user]]])

but I am now trying to limit which fields are selected:

            MyApp.Management.ResourceSubscription
            |> Ash.Query.for_read(:read, %{}, actor: current_user_tenant)
            |> Ash.Query.filter(access_scope: :manage and is_nil(archived_at) and is_nil(resource.archived_at) and user_tenant_id == ^current_user_tenant.id)
            |> Ash.Query.select([:resource_id])
            |> Ash.load(resource: Ash.Query.select(MyApp.Management.Resource, [:id, :slug]))
            |> Ash.read!()

which throws:

[error] GenServer #PID<0.175530.0> terminating
** (ArgumentError) Could not determine a resource from the provided input:

#Ash.Query<resource: MyApp.Management.ResourceSubscription, filter: #Ash.Filter<is_nil(archived_at) and is_nil(resource.archived_at) and user_tenant_id == "40d505c0-8edd-4431-850b-ca794d9c64dd" and access_scope == :manage>, select: [:resource_id, :id]>


    (ash 3.3.3) lib/ash/helpers.ex:386: Ash.Helpers.raise_no_resource_error!/1
    (ash 3.3.3) lib/ash.ex:1699: Ash.load/3

That error should be made much clearer for that particular instance, bug Ash.load is for loading on things that you already have, like a list of records.

You’re looking for Ash.Query.load

            MyApp.Management.ResourceSubscription
            |> Ash.Query.for_read(:read, %{}, actor: current_user_tenant)
            |> Ash.Query.filter(access_scope: :manage and is_nil(archived_at) and is_nil(resource.archived_at) and user_tenant_id == ^current_user_tenant.id)
            |> Ash.Query.select([:resource_id])
            |> Ash.Query.load(resource: Ash.Query.select(MyApp.Management.Resource, [:id, :slug]))
            |> Ash.read!()

Thank you! I’m under my desk crying right now.

Follow up, with a wish: Could the load notation also accept the fields to accept?

e.g. load: [my_relation: [:field1, :field2]]

because typing |> Ash.Query.load(resource: Ash.Query.select(MyApp.Management.Resource, [:id, :slug])) is gonna get really old.

Should be extremely easy to only select the fields they need in a non-verbose way.

Again, thanks!

As of very recently, there is Ash.Query.load(query, :resource, strict?: true) which defaults to selecting nothing (except primary keys).