1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

Revert "Merge pull request #979 from MmKolodziej/better_total_count_for_grouped_queries"

This reverts commit b5e2e07234, reversing
changes made to d1c26fd2fc.

We merged #979 as a performance improvement, but it turns out it
introduced a couple of bugs. The solution seems to be a little tricky as
it has to call `.unscoped` and `unscope(where: :type)`, so let's revert
it until we find a better solution.

closes #1012, #1015.
This commit is contained in:
Yuki Nishijima 2020-02-29 18:24:45 -05:00
parent 73472921a2
commit 04d86ed3f2
3 changed files with 38 additions and 7 deletions

View file

@ -32,12 +32,15 @@ module Kaminari
c = c.limit(max_pages * limit_value) if max_pages && max_pages.respond_to?(:*)
# Handle grouping with a subquery
@total_count = if c.group_values.any?
c.model.from(c.except(:select).select("1")).count
else
c.count(column_name)
end
# .group returns an OrderedHash that responds to #count
c = c.count(column_name)
@total_count = if c.is_a?(Hash) || c.is_a?(ActiveSupport::OrderedHash)
c.count
elsif c.respond_to? :count
c.count(column_name)
else
c
end
end
# Turn this Relation to a "without count mode" Relation.

View file

@ -26,6 +26,12 @@ class Authorship < ActiveRecord::Base
belongs_to :user
belongs_to :book
end
class CurrentAuthorship < ActiveRecord::Base
self.table_name = 'authorships'
belongs_to :user
belongs_to :book
default_scope -> { where(deleted_at: nil) }
end
class Readership < ActiveRecord::Base
belongs_to :user
belongs_to :book
@ -43,6 +49,11 @@ end
class User::Address < ActiveRecord::Base
belongs_to :user
end
class Animal < ActiveRecord::Base; end
class Mammal < Animal; end
class Dog < Mammal; end
class Cat < Mammal; end
class Insect < Animal; end
# a class that uses abstract class
class Product < ActiveRecord::Base
@ -63,9 +74,10 @@ class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migrat
create_table(:users) {|t| t.string :name; t.integer :age}
create_table(:books) {|t| t.string :title}
create_table(:readerships) {|t| t.integer :user_id; t.integer :book_id }
create_table(:authorships) {|t| t.integer :user_id; t.integer :book_id }
create_table(:authorships) {|t| t.integer :user_id; t.integer :book_id; t.datetime :deleted_at }
create_table(:user_addresses) {|t| t.string :street; t.integer :user_id }
create_table(:devices) {|t| t.string :name; t.integer :age}
create_table(:animals) {|t| t.string :type; t.string :name}
end
end
CreateAllTables.up

View file

@ -100,6 +100,22 @@ if defined? ActiveRecord
assert_equal 2, Authorship.group(:user_id).having("COUNT(book_id) >= 3").page(1).total_count
end
test 'calculating total_count with GROUP BY ... HAVING clause with model that has default scope' do
assert_equal 2, CurrentAuthorship.group(:user_id).having("COUNT(book_id) >= 3").page(1).total_count
end
test 'calculating STI total_count with GROUP BY clause' do
{
'Fenton' => Dog,
'Bob' => Dog,
'Garfield' => Cat,
'Bob' => Cat,
'Caine' => Insect
}.each { |name, type| type.create!(name: name) }
assert_equal 3, Mammal.group(:name).page(1).total_count
end
test 'total_count with max_pages does not add LIMIT' do
begin
subscriber = ActiveSupport::Notifications.subscribe 'sql.active_record' do |_, __, ___, ____, payload|