views:

90

answers:

2

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) DESC; & % [])
  end

WTF is it complaining about?

A: 

It looks like a string formatting issue. Try without skipping a line between the first parenthesis and the start of your SQL statement.

JRL
tried that, no change.
Ash
+2  A: 

there's a dot in front of the SELECT statement, are you sure it should be there?

Staelen
That's a copy and paste error - fixed now.
Ash
is it fixed? so what's the error?
Staelen