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 when grouping model has default scope #1012
Fix total_count when grouping model has default scope #1012
Conversation
This PR also fixes our issue: alphagov/whitehall#5300 In our case, we're not using a default scope but a custom one: def unfiltered_scope
StatisticsAnnouncement.includes(:current_release_date, statistics_announcement_topics: :topic, publication: :translations, organisations: :translations)
.joins("INNER JOIN statistics_announcement_dates
ON (statistics_announcement_dates.statistics_announcement_id = statistics_announcements.id)")
.joins("LEFT OUTER JOIN statistics_announcement_dates sd2
ON (sd2.statistics_announcement_id = statistics_announcements.id
AND statistics_announcement_dates.created_at > sd2.created_at)")
.group("statistics_announcement_dates.statistics_announcement_id")
.page(options[:page])
end |
Incorporating solution kaminari#1012 which doesn't cover the special case of STI type restriction.
This is a good catch and should definitely be merged IMO. I've opened #1015 which covers a variation of the same problem (when STI subtypes get involved) and which needed an additional |
This is unscoping |
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
This PR should fix
#total_count
when grouping model hasdefault_scope
.Currently, if you have model like:
and have statement like
User.group(:email).having("COUNT(name) >= 3").page(1).total_count
, you will get error like "Mysql2::Error: Unknown column 'users.deleted_at' in 'where clause'".#total_count
in 1.2.0 uses subquery (here). This remove table reference fromFROM
clause, andWHERE
clause created bydefault_scope
loses table to reference.This PR removes
default_scope
by callingunscoped
to solve this issue.