views:

636

answers:

1

I have a project which needs to use an agregrate function to sort and return relevant records from one of my active record models. Problem is, despite seeing it used on numerous rails tutorials, and despite it being in the online ActiveRecord documentation, my Rails app throws this error at me when the method is called:

Unknown key(s): having

Any ideas why?

I am using it like this (I do have a :group => before it which works, as without the :having the code executes properly, it's just not filtered - which I need.):

Question.find(
  :all,
  :select => "questions.id, questions.text, questions.question_type_id, questions.meta, questions.max_answer_length, CAST(COUNT(form_questions.id) AS REAL) / CAST((SELECT COUNT(*) FROM application_forms) AS REAL) AS expr1",
  :joins => "INNER JOIN form_questions ON questions.id = form_questions.question_id",
  :order => "expr1 DESC",
  :group => "questions.id, questions.text, questions.question_type_id, questions.meta, questions.max_answer_length",
  :having => ["expr1 >= ?", 0.75]
)
+4  A: 

Are you sure that your version of RoR is up to date? ActiveRecord provides support for :having for (i think) 2 years now, but probably you're just using an outdated version?

If you can't update your rails environment you can still use the HAVING clause by adding it to the end of your :group statement.

Best wished, Fabian

halfdan
Perfect, spot on!
Ash
Although I am using v 2.2.2 - but never-mind- it's working now and that's all I care about.
Ash