kaminari icon indicating copy to clipboard operation
kaminari copied to clipboard

Incorrect `total_count` when using `padding`

Open aglushkov opened this issue 3 years ago • 0 comments

Incorrect total_count after association loaded when using padding in the end of list.

If we have 5 comments

puts Comment.page(1).per(2).padding(0).total_count # => 5 # OK
puts Comment.page(1).per(2).padding(0).load.total_count # => 5 # OK

puts Comment.page(1).per(2).padding(2).total_count # => 5 # OK
puts Comment.page(1).per(2).padding(2).load.total_count # => 5 # OK

puts Comment.page(1).per(2).padding(4).total_count # => 5 # OK
puts Comment.page(1).per(2).padding(4).load.total_count # => 1 # WHAT ???

Reproducible script:

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "activerecord", "6.1"
  gem "kaminari-activerecord", "1.2.1"
  gem "sqlite3"
end

require "active_record"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(nil)

ActiveRecord::Schema.define do
  create_table :comments, force: true do |t|
  end
end

class Comment < ActiveRecord::Base
end

5.times { Comment.create! }

puts Comment.page(1).per(2).padding(0).total_count # => 5 # OK
puts Comment.page(1).per(2).padding(0).load.total_count # => 5 # OK

puts Comment.page(1).per(2).padding(2).total_count # => 5 # OK
puts Comment.page(1).per(2).padding(2).load.total_count # => 5 # OK

puts Comment.page(1).per(2).padding(4).total_count # => 5 # OK
puts Comment.page(1).per(2).padding(4).load.total_count # => 1 # WHAT ???

aglushkov avatar Apr 06 '21 13:04 aglushkov