I’m still not 100% with my understanding of the JSON_API layers on Ash.
I’ve created an easy and working aggregate to count the number of related (to_many) records on a resource.
But… I’d like to insert that aggregate value into the JSON api response for gets & reads. I’m currently doing that using the previously learned metadata approach - but I’m curious whether there is a better way to do this.
While I’m at it… Are aggregates the best way to go or should I be using some other form of calculated attributes instead.
Thanks
Martin
Ah, so metadata is not necessary for this.
You can use the fields
query parameter to include public aggregates (i.e aggregates with public?: true
. For example fields[type]=agg_name,other,fields
. If you want the aggregate to be included by default, you can set default_fields
on the route
or the resource level, i.e
default_fields [:the, :list, :of, :fields, :agg_name]
That will make them appear as standard attributes in the response.
1 Like
That’s perfect. Many thanks. The trick (at least to me) was to make the aggregate public and to add it manually to he fields list - or fall back (with no fields list) to the default_fields list.
Thanks again. I’m slowly getting there - step by step.
Martin
1 Like