2016-04-03 54 views
0

我正在從rails 3.2升級到rails 4,並且我無法擺脫這個新錯誤:必須出現在GROUP BY子句中或用於聚合函數--upgrade to rails 4

PG::GroupingError: ERROR: column "answers.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...IN (3778, 3779, 3780, 3783, 3786, 3788) ORDER BY "answers"....^: SELECT sum(CAST(answer AS FLOAT)/(length(questions.possible_answers) - length(replace(questions.possible_answers,';','')) + 1)) as rating FROM "answers" INNER JOIN "questions" ON "questions"."id" = "answers"."question_id" WHERE "answers"."survey_id" = $1 AND ("answers"."created_at" BETWEEN '2016-03-28 00:00:00.000000' AND '2016-04-03 23:59:59.999999') AND "answers"."question_id" IN (3778, 3779, 3780, 3783, 3786, 3788) ORDER BY "answers"."id" ASC LIMIT 1

我的查詢如下:

answer_query = Answer.joins(:question).where(:created_at => start..finish, :question_id => questions_evaluation, :survey_id => survey_id) 
sum = answer_query.select("sum(CAST(answer AS FLOAT)/(length(questions.possible_answers) - length(replace(questions.possible_answers,';','')) + 1)) as rating").first['rating'].to_f 

所以誤差在第二行發生。

我已經嘗試添加unscoped建議here,但後來我得到了一個不同的問題表相關的錯誤。

其他職位已建議刪除外卡,所以我說。選擇(「答案,questions.possible_answers」),但後來我得到

column "answers.*" must appear in the GROUP BY clause or be used in an aggregate function

我不知道什麼嘗試

回答

0

問題在於撥打first我不得不將我的密碼更改爲:

answer_query.select("sum(CAST(answer AS FLOAT)/(length(questions.possible_answers) - length(replace(questions.possible_answers,';','')) + 1)) as rating").limit(1).to_a.first.attributes['rating'].to_f 
相關問題