{@thread.name}

wiggle_wiggle
2023-06-01

wiggle_wiggle:

I have a resource with a count aggregate for a child resource

aggregates do
  count :response_count, :responses
end

I then have a default read action that uses prepare and build to load the aggregate

read :read do
  primary? true
  pagination offset?: true, keyset?: true, required?: false

  prepare build(aggregate: {:response_count, :count, :responses}, load: [:responses])
end

Note that I have currently loaded responses to use the length method to get a count, but whenever I attempt to access response_count it returns an Ash.NotLoaded<:aggregate> value.

Loving ASH btw, It cuts down on a lot of the boilerplate that you get with normal Phoenix contexts.

barnabasj:

I think you have to put, everything into load, at least I have loaded calculations that way. Haven’t really used aggregates so far.

prepare build(load: [:responses, :response_count])

If I’ve understood the documentation correctly you can add an ad-hoc aggregate using the aggregate option (an aggregate that you haven’t already specified on the resource)

wiggle_wiggle:

Yeah, you’re right. Thanks!