Ruby on rails Rails如何从一个查询数据计算多个和

Ruby on rails Rails如何从一个查询数据计算多个和,ruby-on-rails,ruby,activerecord,sum,Ruby On Rails,Ruby,Activerecord,Sum,使用以下两种模型,公司和响应,我将按如下方式查询每个公司的总响应: @allResponses = Company.find(current_user_company_id).responses [#<Response id: 1, company_id: 1, created_at: "2013-04-24 02:36:54", feedback_score: 10, feedback_explanation: "I really like the way you guys do xy

使用以下两种模型,
公司
响应
,我将按如下方式查询每个公司的总响应:

@allResponses = Company.find(current_user_company_id).responses
[#<Response id: 1, company_id: 1, created_at: "2013-04-24 02:36:54", feedback_score: 10, feedback_explanation: "I really like the way you guys do xyz.", additional_data: "", updated_at: "2013-04-24 02:36:54">, #<Response id: 2, company_id: 1, created_at: "2013-04-25 03:51:07", feedback_score: 5, feedback_explanation: "customer service is spotty.", additional_data: "", updated_at: "2013-04-25 03:51:07">, #<Response id: 3, company_id: 1, created_at: "2013-04-25 03:52:04", feedback_score: 7, feedback_explanation: "You've got potential.", additional_data: "", updated_at: "2013-04-25 03:52:04">, #<Response id: 4, company_id: 1, created_at: "2013-04-25 03:52:18", feedback_score: 9, feedback_explanation: "Almost perfect.", additional_data: "", updated_at: "2013-04-25 03:52:18">] 
这为我提供了如下数据:

@allResponses = Company.find(current_user_company_id).responses
[#<Response id: 1, company_id: 1, created_at: "2013-04-24 02:36:54", feedback_score: 10, feedback_explanation: "I really like the way you guys do xyz.", additional_data: "", updated_at: "2013-04-24 02:36:54">, #<Response id: 2, company_id: 1, created_at: "2013-04-25 03:51:07", feedback_score: 5, feedback_explanation: "customer service is spotty.", additional_data: "", updated_at: "2013-04-25 03:51:07">, #<Response id: 3, company_id: 1, created_at: "2013-04-25 03:52:04", feedback_score: 7, feedback_explanation: "You've got potential.", additional_data: "", updated_at: "2013-04-25 03:52:04">, #<Response id: 4, company_id: 1, created_at: "2013-04-25 03:52:18", feedback_score: 9, feedback_explanation: "Almost perfect.", additional_data: "", updated_at: "2013-04-25 03:52:18">] 
试试这个

 @sumOfHighScores = @allResponses.select{ |response| response.feedback_score > 8 }.sum
 @sumOfLowScores = @allResponses.select{ |response| response.feedback_score < 7 }.sum
@sumOfHighScores=@allResponses.select{| response | response.feedback_scores>8}.sum
@sumOfLowScores=@allResponses.select{| response | response.feedback_score<7}.sum
试试这个

 @sumOfHighScores = @allResponses.select{ |response| response.feedback_score > 8 }.sum
 @sumOfLowScores = @allResponses.select{ |response| response.feedback_score < 7 }.sum
@sumOfHighScores=@allResponses.select{| response | response.feedback_scores>8}.sum
@sumOfLowScores=@allResponses.select{| response | response.feedback_score<7}.sum
您可以试试这个

@sumOfHighScores = @allResponses.select{ |response| response.feedback_score > 8 }.map(&:feedback_score).sum
@sumOfLowScores = @allResponses.select{ |response| response.feedback_score < 7 }.map(&:feedback_score).sum
@sumOfHighScores=@allResponses.select{| response | response.feedback_scores>8}.map(&:feedback_scores).sum
@sumOfLowScores=@allResponses.select{| response | response.feedback_score<7}.map(&:feedback_score).sum
您可以试试这个

@sumOfHighScores = @allResponses.select{ |response| response.feedback_score > 8 }.map(&:feedback_score).sum
@sumOfLowScores = @allResponses.select{ |response| response.feedback_score < 7 }.map(&:feedback_score).sum
@sumOfHighScores=@allResponses.select{| response | response.feedback_scores>8}.map(&:feedback_scores).sum
@sumOfLowScores=@allResponses.select{| response | response.feedback_score<7}.map(&:feedback_score).sum

我将在数据库中执行整个计算

company = Company.find(current_user_company_id)
totals =  company.responses.sum(
            :feedback_score,
            :group => "CASE WHEN feedback_score < 7 THEN 'low' ELSE 'high' END")
low, high = (totals['low'] || 0), (totals['high'] || 0 )
company=company.find(当前用户公司id)
总计=company.responses.sum(
:反馈和分数,
:组=>“反馈时的案例(分数<7,然后为‘低’或‘高’端”)
低,高=(总计['low']| 0),(总计['high']| 0)

我将在数据库中执行整个计算

company = Company.find(current_user_company_id)
totals =  company.responses.sum(
            :feedback_score,
            :group => "CASE WHEN feedback_score < 7 THEN 'low' ELSE 'high' END")
low, high = (totals['low'] || 0), (totals['high'] || 0 )
company=company.find(当前用户公司id)
总计=company.responses.sum(
:反馈和分数,
:组=>“反馈时的案例(分数<7,然后为‘低’或‘高’端”)
低,高=(总计['low']| 0),(总计['high']| 0)

我选择你的,因为这样我就可以做额外的/不相关的数学,而无需再做另一个查询。我选择你的,因为这样我就可以做额外的/不相关的数学,而无需再做另一个查询。