'未知密钥:有'在 Ruby on Rails ActiveRecord 中查找方法

发布于 2024-08-13 07:27:55 字数 841 浏览 4 评论 0原文

我有一个项目需要使用聚合函数从我的活动记录模型之一中排序并返回相关记录。问题是,尽管看到它在许多 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故乡的云 2024-08-20 07:27:55

您确定您的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文