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
Conversation
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.
…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.
closed by 04d86ed |
I have reverted #979 and picked up the test from this PR. Thanks for reporting this issue! |
Improved performance for total_count on grouped ActiveRecord::Relation. This re-implements kaminari#979 using Arel to avoid issues kaminari#1012 and kaminari#1015
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
Improved performance for total_count on grouped ActiveRecord::Relation. This re-implements kaminari#979 using Arel to avoid issues kaminari#1012 and kaminari#1015
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
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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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":
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 |
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