use an AR query to get a grouped count instead of custom SQL

refs CNVS-7032

primarily, Mysql's execute doesn't return rows, but this is much
much cleaner anyway

Change-Id: Ia8c13374e5625ae0568f9bc386101dd2a2048d88
Reviewed-on: https://gerrit.instructure.com/28620
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Nick Cloward <ncloward@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Cody Cutrer 2014-01-15 13:29:31 -07:00
parent b15620591a
commit 81a63f2922
1 changed files with 2 additions and 10 deletions

View File

@ -298,15 +298,7 @@ class CollectionsController < ApplicationController
end
def preload_groups_collections_counts(groups)
counts_data = Collection.connection.execute(Collection.send(:sanitize_sql_array, [<<-SQL, groups.map(&:id)])).to_a
SELECT context_id AS group_id, COUNT(*) AS collections_count
FROM collections
WHERE context_id IN (?) AND context_type='Group' AND workflow_state='active'
GROUP BY context_id
SQL
@collections_counts = {}
counts_data.each do |cd|
@collections_counts[cd['group_id'].to_i] = cd['collections_count'].to_i
end
@collections_counts = Collection.where(context_id: groups, context_type: 'Group', workflow_state: 'active').
group(:context_id).count
end
end