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:
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...
Jump to solution
10 Replies
ZachDaniel
ZachDaniel6mo ago
you can indeed:
has_many ... do
limit 10
sort foo: :asc
end
has_many ... do
limit 10
sort foo: :asc
end
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 😜
calculate :count_of_last_ten_posts_that_are_unpublished, :integer, expr(
count(posts, query: [limit: 10, sort: [inserted_at: :desc], filter: published == true])
)
calculate :count_of_last_ten_posts_that_are_unpublished, :integer, expr(
count(posts, query: [limit: 10, sort: [inserted_at: :desc], filter: published == true])
)
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 😄
pikdum
pikdumOP6mo ago
* Cannot set limit on aggregate query
hm 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 type
ZachDaniel
ZachDaniel6mo ago
huh. Maybe I'm wrong
pikdum
pikdumOP6mo ago
i'll make a proposal for limits + sort on a has_many relationship
ZachDaniel
ZachDaniel6mo ago
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 queries
pikdum
pikdumOP6mo ago
https://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
ZachDaniel
ZachDaniel6mo ago
Yeah, that would work. You could also see what happens if you do this:
has_many :last_ten... do
read_action :an_action_with_a_limit
end
has_many :last_ten... do
read_action :an_action_with_a_limit
end
nicolkill
nicolkill5mo ago
A question here, my context
# action on the relation
read :latest_filterable do
filter expr(exists(custom_field, filterable == true))
prepare build(limit: 10)
end

# main module

# calculation
calculate :custom_field_text, :string, expr(filterable_custom_fields.value) do
load [:filterable_custom_fields]
public? true
end

# relation
has_many :filterable_custom_fields, CustomField
read_action :latest_filterable
source_attribute :id
destination_attribute :custom_field_id
writable? false
public? true
end
# action on the relation
read :latest_filterable do
filter expr(exists(custom_field, filterable == true))
prepare build(limit: 10)
end

# main module

# calculation
calculate :custom_field_text, :string, expr(filterable_custom_fields.value) do
load [:filterable_custom_fields]
public? true
end

# relation
has_many :filterable_custom_fields, CustomField
read_action :latest_filterable
source_attribute :id
destination_attribute :custom_field_id
writable? false
public? true
end
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)
ZachDaniel
ZachDaniel5mo ago
THis seems like a different question? oh, sounds like you worked it out.
nicolkill
nicolkill5mo ago
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

Did you find this page helpful?