Is there a way to do a 'limit' on a relationship?
My use case is eventually an aggregate, like 'count of the last 10 posts that are unpublished'.
Was thinking I could get something working if I set up a relationship that was 'most recent 10 posts', and then an aggregate count on that relationship, but not seeing anything on how that'd work.
Any ideas?
Solution:Jump to solution
Yeah, that would work. You could also see what happens if you do this:
```elixir
has_many :last_ten... do
read_action :an_action_with_a_limit...
10 Replies
you can indeed:
oh, wait
I guess you can't 🤔
I really thought you could do that 😓
but you can limit an aggregate FWIW
but apparently only the inline calculation type of aggregate. Wow, how does everyone ask something innocent and stumble into a mess 😜
Something like this should work though
And thank you for being the first person to use the new forum setup ❤️
We could support
limit
on aggregate DSL and on has_many
relationships though FWIW
Feel free to open proposals for both 😄* Cannot set limit on aggregate queryhm i like the idea of doing the limit on a
has_many
relationship, that seems pretty clean
https://github.com/ash-project/ash/blob/f6688fcc71b750f8d96222ec8eb157b0eff523ea/lib/ash/query/aggregate.ex#L523
getting this even if it's an inline calculation type aggregate rather than an aggregate count typehuh. Maybe I'm wrong
i'll make a proposal for limits + sort on a has_many relationship
right-o
you can't do it apparently 😢
You can definitely do
sort
just not limit apparently. I'll have to look into why limit
isn't supported on aggregate querieshttps://github.com/ash-project/ash/issues/2012 - made this
so I think right now my best bet might be to just do things through code, like:
* read action for getting last 10 posts that are unpublished
* Enum.count or similar
Solution
Yeah, that would work. You could also see what happens if you do this:
A question here, my context
on this case i have my action on my relation module, on the main module the relation and a calculation that uses this relation
and i have a issue of forbidden error, can you help me here please
forgot that, the issue was use the source/destination attributes, removing works well (the module has already that data)
THis seems like a different question?
oh, sounds like you worked it out.
was about the same, use a limit on a relation, using the
has_many > read_action
but returns a forbidden, but was about my config, not about the action