Comments (9)
OK, so the :all
IS necessary, but it may not work with "custom select statements", or group, order etc., right? That's a quite old AR problem AFAIR.
It also performs two SQL queries
Standard pagination requires knowing the count, and unluckily the count doesn't come with the results in most DBs, but requires an extra query.
If avoiding one query is a priority, Pagy::Countless
can help (if your pagination requirements can accommodate its limitations).
I am contented with the workaround in the issue description
If the count is right, it looks like the simplest solution to me!
from pagy.
Pagy::Countless
Countless extra
from pagy.
@AbhayVAshokan Thank you for the report and the solution ;)
Pagy strives to be as agnostic as possible, hence it "shouldn't" know much about ActiveRecord, and it does not by design.
However, since AR is overwhelmingly used in most apps, pagy tries to be as "AR friendly" as possible, as you can notice in the code that it runs on ANY collection in order to get the collection count:
Line 21 in 42b99f8
That's hardly a one-size-fits-all solution, but it works in an overwhelmingly number of cases. Indeed you may need to override the method with apps using other ORMs (usually by simply removing the :all
argument). In cases where the collection.count(:all)
does not work, you can pass your own count to the helper as explained here.
What would be the right ruby statement to get the right count from the pages_and_block_counts
in your alias case?
from pagy.
I can consider the possibility to add a :count_args
pagy variable, which could be used to pass the arguments to the count
. Defaut count_args: [:all]
. That would allow things like:
@pagy, @records = pagy(my_collection, count_args: ['age'])
or set its default to an empty array, for the ORMs that use count with no argument.
Would that be helpful also in your case?
from pagy.
Thank you for the prompt responses. I am assuming that count
is used to set the upper limit during pagination. In my case setting count as Page.count
gets the work done.
from pagy.
I am a bit rusty on ActiveRecord, and I forgot why we have to add :all
to the statement in the past, but there was some problem with not passing anything to count
AFAIR.
It's just a matter of picking the right default at this point, since :count_args
will definitely be part of the incoming 7.0.
Maybe things changed more recently and we could remove :all
?
from pagy.
from pagy.
Passing :all
arg to the count
method is necessary. The original query can have custom select statements. The COUNT
operation will not perform correctly in all these situations.
For instance, in my example, a count
without :all
will try to perform COUNT(pages.name, COUNT(blocks.id) AS blocks_count) AS "count_pages_name_count_blocks_id_as_blocks_count"
and fail miserably.
from pagy.
I took some time to understand the problem better. I think this is how I should write a custom count operation:
- pagy(pages_and_block_counts)
+ count = pages_and_block_counts.unscope(:order).count(:all).size
+ pagy(pages_and_block_counts, count: count)
- Remove the
order
query. - Perform a
count(:all)
operation. - Since
count(:all)
returns a hash, perform asize
operation.
This will execute two SQL queries
Page Count (1.1ms) SELECT COUNT(*) AS "count_all", "pages"."id" AS "pages_id" FROM "pages" LEFT OUTER JOIN "blocks" ON "blocks"."deleted_at" IS NULL AND "blocks"."page_id" = "pages"."id" GROUP BY "pages"."id"
Page Load (0.8ms) SELECT pages.name, COUNT(blocks.id) AS blocks_count FROM "pages" LEFT OUTER JOIN "blocks" ON "blocks"."deleted_at" IS NULL AND "blocks"."page_id" = "pages"."id" GROUP BY "pages"."id" /* loading for pp */ ORDER BY "pages"."sequence" ASC, blocks_count DESC LIMIT $1 OFFSET $2 [["LIMIT", 11], ["OFFSET", 0]]
I am contented with the workaround in the issue description. It also performs two SQL queries.
Adding a count_args
will not be helpful in my case :(
from pagy.
Related Issues (20)
- Add ARIA attributes to semantic helper
- Add ARIA attributes to uikit helper
- Bug: pagy_search wrong number of arguments HOT 10
- Bug: Issue with Arel scope passed to pagy method HOT 3
- Docs: 7.0 has an undocumented breaking change for active page styling HOT 5
- Docs: Which branch should PRs be made against? HOT 3
- Docs: Update performance metrics HOT 5
- Trim doesn't call with pagy_nav_js when first page is active
- Bug: Adapt with Mongoid add extention HOT 1
- Bug: Pagy::Backend#pagy_get_vars `count_args` gets ignored HOT 3
- Docs: https://ddnexus.github.io/pagy/quick-start/#configure HOT 1
- Bug: Missing initializer in documentation HOT 1
- Bug: wrong number of arguments (given 2, expected 0) HOT 2
- Bug: wrong number of arguments (given 2, expected 0) HOT 1
- Bug: Pagy not working with Geocoder gem
- Bug: Undocumented backwards-incompatible change of empty "page" parameter introduced in 6.4.0 HOT 3
- Docs: lost section about wrap existing pagination with pagy_calendar HOT 1
- Bug: Request for Pagy Gem: Making Commands Platform Independent HOT 1
- Bug: Pagy 8.4.0 is broken with MS SQL server HOT 18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pagy.