'未知密钥:有'在 Ruby on Rails ActiveRecord 中查找方法
我有一个项目需要使用聚合函数从我的活动记录模型之一中排序并返回相关记录。问题是,尽管看到它在许多 Rails 教程中使用,并且尽管它位于在线 ActiveRecord 文档中,但我的 Rails 应用程序在调用该方法时向我抛出此错误:
Unknown key(s): having
有什么想法吗?
我像这样使用它(我在它之前确实有一个 :group =>
,它可以工作,因为如果没有:让代码正确执行,它只是没有被过滤 - 这是我需要的。):
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]
)
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]
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定您的 RoR 版本是最新的吗? ActiveRecord 提供了对 :having 的支持(我认为)已经有 2 年了,但您可能只是使用过时的版本?
如果您无法更新 Rails 环境,您仍然可以通过将 HAVING 子句添加到 :group 语句的末尾来使用它。
最好的祝愿,
法比安
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