I am upgrading from rails 3.2 to rails 4 and I this new error I have not been able to get rid off:
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
My query is the following:
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
So the error is happening in the second line.
I have tried adding unscoped as suggested here but then I get a different error related to the questions table.
Other posts have suggested removing wild cards, so I added .select("answers, questions.possible_answers"), but then I get
column "answers.*" must appear in the GROUP BY clause or be used in an aggregate function
I am not sure what else to try