Aggregate first the full resource instead of some field
I have a property resource that can contain offers.
When I'm fetching the property, I want to also fetch the current offer from the actor if there is one.
Right now, I have this:
But this will not work since the first aggregation requires me to select a field from
:offers
, but I want it to actually select the whole offer.
Is there some way for me to do this with aggregates?21 Replies
no need for an aggregate there
Ah, that's cool!
So, I added this:
But the whole thing exploded during query compilation 😅
Seems to be related to the
^actor(:id)
bitoh
yeah sorry
you can't use the actor in either filter
You're going to want a calculation for this
(sorry about that)
I think calculate has arity 3 right? Seems like it expects the return type as the second argument, I tried adding the
Offer
object to it but seems like that is not correct, I will take a look at the docs
Yeah.. I don't get it.
I'm adding the calculation this way:
But I get this error
From what I saw from other examples that should be the correct way to add the return type as the resourceYeah, that looks right to me, not sure what is going on there...
oh
actually one sec
ash main should work for you now
Hm, I got the same error
hm...
you sure you updated?
I just pushed it a second ago
let me try again
are you sure its the same exact error?
how did you update to main?
you set it to
github: "ash-project/ash"
? did mix deps.update ash
?Yep
🤔
Can you comment out your calculation
and in iex do this
actually nvm
I see whats going on
okay try main again
Yep! Now it is compiling 😁
So, should I expect the calculation to have the actor inside the context (last field of
calculate
function) by default if the action itself was called with the actor set?
Because I'm not getting it😢 I keep leading you astray with this.
The actor is not passed to calculations
There is an open ticket for this to be fixed
So I guess for now there is no good way to calculate extra field in the resource that requires an actor?
lemme try to fix it, one sec
alright, try
main
the actor
should be in the context nowYep, now I can see the actor! Amazing work as always!
So, last question (hopefully) about this.
I see that Ash is smart when doing queries that need to fetch data from a lot of resources, it sends the resource's ids as a list and fetches data to all of them in a single query.
I don't want to do one query per resource inside the
calculate
function, so can you direct me to some code, or where Ash does that single query so I can use as reference and do the same with my own query for this calculation?The calculation function takes a list of records and returns a list of results
So that you can do exactly that same kind of thing
i.e get the ids of all the records being loaded, and then fetch only the relevant records, and then return them properly
i.e
All right! So here is my solution in case someone wants to use it as a reference:
In my Offer resource, I created an action to retrieve offers from a list of properties_ids:
On my Property resource, I added this calculation:
And here is the calculation implementation: