Can anyone please help me with this SQL Query (works in SQLite DB Browser, but not in Rails)
Rails has been spitting this out to me:
SQLite3::SQLException: near "SELECT": syntax error:
SELECT questions.id, questions.text, questions.question_type_id, questions.meta, questions.max_answer_length,
COUNT(form_questions.id) AS expr1,
(5) AS expr2,
CAST(COUNT(form_questions.id) AS REAL) / CAST((5) AS REAL) AS expr3
FROM questions, form_questions
WHERE form_questions.question_id = questions.id
GROUP BY questions.id, questions.text, questions.question_type_id, questions.meta, questions.max_answer_length
HAVING expr3 >= 0.75
ORDER BY COUNT(form_questions.id) DESC;
The query executes correctly with no problems in the SQLite Database Browser, but for some reason Rails is baulking at it.
The code is:
def self.find_by_commonality
Question.find_by_sql(%&
SELECT questions.id, questions.text, questions.question_type_id, questions.meta, questions.max_answer_length,
COUNT(form_questions.id) AS expr1,
(5) AS expr2,
CAST(COUNT(form_questions.id) AS REAL) / CAST((5) AS REAL) AS expr3
FROM questions, form_questions
WHERE form_questions.question_id = questions.id
GROUP BY questions.id, questions.text, questions.question_type_id, questions.meta, questions.max_answer_length
HAVING expr3 >= 0.75
ORDER BY COUNT(form_questions.id) 开发者_StackOverflow社区DESC; & % [])
end
WTF is it complaining about?
there's a dot in front of the SELECT statement, are you sure it should be there?
It looks like a string formatting issue. Try without skipping a line between the first parenthesis and the start of your SQL statement.
精彩评论