-
Notifications
You must be signed in to change notification settings - Fork 412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for counting grouped collections #51
Conversation
Thanks @sliminas. I need to understand it better: couldn't that be avoided by grouping after the pagination? Please, could you post a specific example and use case? Thanks. |
I also just ran into this problem. It looks like kaminari covers this situation interally. |
Can you also update this for the extras as well? they override the |
To me this looks like quite inefficient: you get it counted WITH distinct so it has to split the total count, and then re-join it, not to mention that you have to check every time whether it is one thing or the other... which doesn't come for free. To me counting the full collection with distinct just to have a plain integer looks a bit off. I asked before and I ask it again because I didn't try it: couldn't that be avoided by groping AFTER the pagination (which would mean that you get a plain count, without splitting and re-joining, and you finally get what you want when you need it? Thanks |
@KevinColemanInc there is only one extra overriding |
@KevinColemanInc My solution for this was directly taken from how kaminari handles that. |
@ddnexus The setup I have: class ProductsController < ApplicationController
include Pagy::Backend
def index
# .... build some other objects depending on the user input
@products = ProductFilter.filter(dynamic_filter_and_search_variables_and_objects)
# @products sometimes is a grouped ActiveRecord::Relation
@pagy, @products = pagy(@products)
end
end The grouped collection I get is returned from a So I would have to copy all the logic from |
Basically the same story for me too. Having the filter/finder classes also have to calculate their own count would be annoying to say the least. At least for now, in my app, I would rather take the performance hit than spend the engineering hours to update all of the filters/finder classes to create a query for the results and a query for the count, unless there is an easy way to dynamically create that query across all group by queries. |
@sliminas Thanks for the example BTW, talking about an easy way that doesn't need any PR. It would be enough just defining an alias in the initializer like: class Hash; alias :to_i :count; end That would avoid checking for all the count results. I don't see any practical downside, and in this context it even makes sense: calling |
That makes sense. Maybe just update the docs then? |
@KevinColemanInc The problem I still have with my own suggestion is that a grouped collection should be a case covered out of the box, because the generic That is practical and elegant, makes sense, and doesn't hurt anything, but it is something that I would like to weight a bit more. On the other hand leaving it up to the user (maybe adding it in the initializer_example.rb) would not cover all the AR cases by default. I am currently exploring other possibilities. Let's see if we could find a better way... at worse we have already 2 working solutions. |
The alternatives I tried were not so great. Adding a The original proposed PR has been merged: I just changed the condition using @sliminas @KevinColemanInc Thanks for the PR and the feedback. |
The PR only fixes it in the default case, not in the add-ins that monkey patch that method. Could we also get this applied to them as well? |
What "add-ins" are you talking about? |
I meant "extras", I didn't realize that the "parent" method was being called via alias_method: https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/items.rb#L16 nevermind |
Ah, OK. I was confused because I already told you that a few days ago:
|
This PR solves the
undefined method 'to_i' for Hash
exception for grouped AR collections (#50).