Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: total_count with GROUPED STI relation #1015

Closed
wants to merge 2 commits into from

Conversation

bendilley
Copy link
Contributor

When querying a subtype on a STI table, a 'hard scope' is applied that
is incompatible with the use of model.from(subquery)

This fix removes any type restriction from the outer query (because
the subquery already has the restriction).

Introduced by #979

When querying a subtype on a STI table, a 'hard scope' is applied that
is incompatible with the use of `model.from(subquery)`

This fix removes any type restriction from the _outer_ query (because
the subquery already has the restriction).

Introduced by kaminari#979
Incorporating solution kaminari#1012 which doesn't cover the
special case of STI type restriction.
yuki24 added a commit that referenced this pull request Feb 29, 2020
…or_grouped_queries"

This reverts commit b5e2e07, reversing
changes made to d1c26fd.

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.
@yuki24
Copy link
Member

yuki24 commented Feb 29, 2020

closed by 04d86ed

@yuki24
Copy link
Member

yuki24 commented Feb 29, 2020

I have reverted #979 and picked up the test from this PR. Thanks for reporting this issue!

bendilley added a commit to skillstream/kaminari that referenced this pull request May 13, 2020
Improved performance for total_count on grouped ActiveRecord::Relation.

This re-implements kaminari#979 using Arel to avoid issues kaminari#1012 and kaminari#1015
bendilley added a commit to skillstream/kaminari that referenced this pull request May 13, 2020
Improved performance for total_count on grouped ActiveRecord::Relation.

This re-implements kaminari#979 using plain SQL to avoid issues kaminari#1012 and kaminari#1015
bendilley added a commit to skillstream/kaminari that referenced this pull request May 13, 2020
Improved performance for total_count on grouped ActiveRecord::Relation.

This re-implements kaminari#979 using Arel to avoid issues kaminari#1012 and kaminari#1015
bendilley added a commit to skillstream/kaminari that referenced this pull request May 29, 2020
Improved performance for total_count on grouped ActiveRecord::Relation.

This re-implements kaminari#979 using plain SQL to avoid issues kaminari#1012 and kaminari#1015
bendilley added a commit to bendilley/kaminari that referenced this pull request Oct 29, 2020
Improved performance for total_count on grouped ActiveRecord::Relation.

This re-implements kaminari#979 using Arel to avoid issues kaminari#1012 and kaminari#1015
@@ -34,7 +34,8 @@ def total_count(column_name = :all, _options = nil) #:nodoc:

# Handle grouping with a subquery
@total_count = if c.group_values.any?
c.model.from(c.except(:select).select("1")).count
# STI can place a 'hard scope' on the model which doesn't work on the outer query here
c.model.unscoped.unscope(where: :type).from(c.except(:select).select("1")).count
Copy link

@guigs guigs Nov 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use unscoped.unscope(:where) because one can use different column for STI (using inheritance_column)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further down @yuki24's "rabbit hole"!

Here's another idea which you could argue reads better than "unscope it and then unscope it some more":

Suggested change
c.model.unscoped.unscope(where: :type).from(c.except(:select).select("1")).count
c.model.base_class.unscoped.from(c.except(:select).select("1")).count

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants