Code Monkey home page Code Monkey logo

Comments (3)

amatsuda avatar amatsuda commented on May 13, 2024

Yes, I agree that limit + offset + count query returns something unexpected, but it's because that's the way SQL acts.

SELECT COUNT(*) FROM "users" 

=> 100

SELECT COUNT(*) FROM "users" LIMIT 10 OFFSET 10

=> 0

Kaminari and AR and Arel just uses this as it is, thus the following query returns 0.

User.limit(10).offset(10).count

If you want this query to return something different, I'm sorry but that's not Kaminari's matter. I think we should improve ActiveRecord or Arel.
You said, you expect count to return total_count, but I guess some people might expect the same value with length rather than total_count.

Anyway, to work around this ATM, you can use length or all.count for the limited list count, and total_count or except(:offset, :limit).count for the whole record count.
As for any?, exists? or all.any? would work as expected.

from kaminari.

cannikin avatar cannikin commented on May 13, 2024

Yeah, makes sense. I can see where most of the time you want count to return the real count of records you've pulled from the DB, not the total number of records that exist in the DB. total_count was a good compromise, I've updated my code to use that. Thanks for getting back so quick!

from kaminari.

henrik avatar henrik commented on May 13, 2024

Same issue with present?

from kaminari.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.