Skip to content
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

Pagy does not allow using custom selects #130

Closed
bobbytables opened this issue Feb 25, 2019 · 2 comments
Closed

Pagy does not allow using custom selects #130

bobbytables opened this issue Feb 25, 2019 · 2 comments

Comments

@bobbytables
Copy link

I'm using this activerecord query:

r = scope.incidents.select('incidents.*, array_position(ARRAY[0, 1], id) AS priority')
r.reorder('priority asc', created_at: :asc).group('incidents.id')

This fails when pagy attempts to get the count for the incidents table though because it includes the order statement but not the custom select:

Query generated by Pagy:

SELECT COUNT(*) AS count_all, incidents.id AS incidents_id FROM "incidents" WHERE "incidents"."organization_id" = $1 GROUP BY incidents.id ORDER BY priority asc, "incidents"."created_at" ASC

IMO order shouldn't matter for a count call right? Probably should just do reorder(nil) on the count call in pagy?

@ddnexus
Copy link
Owner

ddnexus commented Feb 26, 2019

As you said, that is a custom select that needs a custom way to get the count (which pagy cannot know).

You have a few options to deal with that. The simples is getting your custom count and pass it as a variable:

your_scope = ...
your_count = ...
@pagy, @records = pagy(your_scope, count: your_count)

If you have to repeat the same algorithm in many actions, you may consider extracting it as a method, or you can even override the pagy_get_vars method in your controller, and change the way it gets the count.

I will add a section in the How to page. Thanks.

@bobbytables
Copy link
Author

Great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants