working on aggregating results

This commit is contained in:
Jared Cosulich 2017-03-13 12:59:13 -04:00
parent 5d36ab0a62
commit 8c389742ee
7 changed files with 44 additions and 15 deletions

View file

@ -33,7 +33,7 @@ class Attempt < ApplicationRecord
if school_category.nil?
school_category = SchoolCategory.create(school: recipient.school, category: question.category)
end
# school_category.aggregate_responses
school_category.sync_aggregated_responses
end
end

View file

@ -3,6 +3,9 @@ class SchoolCategory < ApplicationRecord
belongs_to :school
belongs_to :category
validates_associated :school
validates_associated :category
scope :for, -> (school, category) { where(school: school).where(category: category) }
def aggregated_responses
@ -14,9 +17,9 @@ class SchoolCategory < ApplicationRecord
select('sum(attempts.answer_index) as answer_index_total')[0]
return {
attempt_count: attempt_data.attempt_count,
response_count: attempt_data.response_count,
answer_index_total: attempt_data.answer_index_total
attempt_count: attempt_data.attempt_count || 0,
response_count: attempt_data.response_count || 0,
answer_index_total: attempt_data.answer_index_total || 0
}
end