Comments (11)
OK, after a .page(x)
that's true, it only fetch 10 records. But I don't agree with you on other point. I ensure that .all
in rails always performs an SQL query.
Note that the issue is due to scopes created by kaminari and count
. count
just can't work correctly while records are not fetched. That's not a kaminari issue. Here is an explanation:
articles = Article.page(42).per(10)
articles.count # 0 (because it generates makes a "SELECT COUNT(*) LIMIT x OFFSET x" query
articles.size # 0 (the same as count)
articles.empty? # true (because it uses count)
articles.any? # false (because it uses count too)
articles.all.size # 10 (but it fetches records)
articles.except(:limit, :offset).count # yeah, 142 (without fetching records)!
articles.total_count # same as before (kaminari internal)
You may figure that the solution is to invoke #all
method before, but,... no:
articles = Article.page(42).per(10).all
articles.count # 10 (array method)
articles.total_count # NoMethodError (and required by view helper!).
A workaround for kaminari is to dynamically override #count
(after invoking #page
method) without limit
and offset
(exactly like total_count) in order to have #empty?
, #any?
with correct behavior.
WDYT?
from kaminari.
This is due to OFFSET
and LIMIT
in count query. Have a look to your logs: SELECT COUNT(*) FROM "table" LIMIT x OFFSET x
. There is other issues tickets about it. I'll try to provide a patch.
from kaminari.
alexistoulotte;
Yeah I found other tickets with the same issue, for now I'm using the .all.count workaround and is working.
Thanks
from kaminari.
IMHO .all.count
is absolutely not a solution: if you have 10 000
records, it fetch and instanciate all of them.
from kaminari.
No, that is not true. Model.limit(10).offset(x).all
fetches only 10 records.
And you're probably using all of these 10 records somewhere in the view anyway, so I think fetching them by all
for count
is not a very bad idea.
from kaminari.
@alexistoulotte after calling .page(x) and .all.count, there is no other sql query, but my variable gets the expected objects from .page call.
I would like that only calling .page(x) could be enough to get my objects, but right now this allow me to use kaminari.
from kaminari.
Yes, I understand that AR .limit
+ .count
is somewhat broken ATM.
Actually that problem is already reported as a bug on rails.lighthouseapp https://rails.lighthouseapp.com/projects/8994/tickets/5060
So, I suppose that would be improved sooner or later.
The best solution I found so far is this patch rails/rails#201
so I will try freedom patching .count
method with this approach inside Kaminari.
Thanks!
from kaminari.
OK, cool :)
Will monkey patch for the moment until next rails release.
Thanks!
from kaminari.
Experiencing the same issue. I am doing:
<% @comments.entries.each do |comment| %>
Instead of:
<% @comments.each do |comment| %>
Which seems to work for now. Please keep me posted as to when an official patch is realeased
from kaminari.
I'm having problems with this. Kaminari is only showing the first page of results for me, "SELECT COUNT(*) FROM xxx
LIMIT 25 OFFSET 25" in the logs. Running rails 3.0.6, kaminari 0.10.4. Heard good things about kaminari but this is really stuck for me. Hopefully there will be a good fix soon.
from kaminari.
This is actually already fixed in edge Rails
rails/rails@d5994ee
rails/rails@28c73f0
but I guess these commits won't be backported to 3.0 branch.
So, I put a workaround for this in 0.11.0 gem 465a2d0
Let's see how this works...
from kaminari.
Related Issues (20)
- rails g kaminari:views bulma does not work on Ruby 3 HOT 2
- enter custom page number HOT 3
- rails g kaminari:views doesn't generate anything HOT 12
- Hi HOT 1
- undefined local variable or method `paginator' for _page.html.erb:22 partial HOT 2
- Undefined method `without_count' HOT 2
- Update wiki link in GitHub "About" section HOT 1
- issue on method_missing due to separation of positional and keyword arguments in Ruby 3
- Is it possible to add 'data-turbo-action' to the paginate helper? HOT 4
- How to use "Creating Friendly URLs and Caching" in "member do" ?
- With the update from i18n to v1.11.0 `:zero` in not longer supported as key
- Problem Monkey Patching Rails 7.0
- render error for js.erb
- when large table query, use total_count and total_pages slow
- paginate view helper is failing with includes HOT 3
- Updating content_for blocks
- `link_to_first_page` / `link_to_last_page` ?
- inconsistent results in case of LEFT OUTER JOIN, GROUP BY and HAVING HOT 2
- Array doesn't get paginated when total count is passed in HOT 1
- Ambiguous column "id" when PG COUNT is used with certain joins HOT 1
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 kaminari.