Filtering on embedded resource?

I’m trying to filter an ash resource based on an embedded resource. Eg. Post → User (embedded). On AshJsonApi i’m filtering like so filter[post][user][some_field]=some_id but getting an Invalid reference error.

I tried creating a custom preparation below to take advantage of the custom filter but its still returning the same error.

      prepare fn query, _ ->
        case Ash.Changeset.get_argument(query, :filter) do
          %{"user" => user} ->
            member_id = user["member_id"]

            Ash.Query.filter(
              query,
              post.user.member_id == ^member_id
            )

          _ ->
            query
        end

Not sure how ash filters are supposed to work with embedded resources and any pointers here would help.

I think we’d need to do some work to support this from the api level, but in your query the thing you need to change is use brackets for accessing embeds.

Ash.Query.filter(
  query,
  post.[:user][:member_id] == ^member_id
)

Still getting “Invalid reference post”.

oh, right, the resource is the post. You can access the variables directly

Ash.Query.filter(
  query,
  user[:member_id] == ^member_id
)

That seems to work! But now Im getting %Ash.Error.Query.NoSuchFilterPredicate{key: “member_id”}. Do I have to set anything up in the embedded resource for querying?

:thinking: That sounds strange. Is that from passing filters in via json api?

Yeah, strange

Yeah, so I don’t think it’s a valid filter in ash_json_api currently. AshJsonApi would need to be made aware of embedded resources to allow filtering on them.