命名范围 +平均值导致在 postgresql 上运行的 sql 查询中多次指定表

发布于 2024-09-01 14:43:32 字数 1471 浏览 4 评论 0原文

我有一个像这样的命名范围...

  named_scope :gender, lambda { |gender| { :joins => {:survey_session => :profile }, :conditions => { :survey_sessions => { :profiles => { :gender => gender } } } } }

当我调用它时一切正常。

我也有一个我称之为的平均方法...

Answer.average(:rating, :include => {:survey_session => :profile}, :group => "profiles.career")

如果我这样称呼它,它也可以正常工作。

但是,如果我这样称呼它......

Answer.gender('m').average(:rating, :include => {:survey_session => :profile}, :group => "profiles.career")

我得到......

ActiveRecord::StatementInvalid: PGError: 错误: 多次指定表名“profiles” :选择avg(“answers”. rating)AS avg_ rating,profiles.career AS profile_career FROM“answers”LEFT OUTER JOIN“survey_sessions”survey_sessions_answers ON“survey_sessions_answers”.id =“answers”.survey_session_id LEFT OUTER JOIN“profiles”ON“profiles” ".id = "survey_sessions_answers".profile_id INNER JOIN "survey_sessions" ON "survey_sessions".id = "answers".survey_session_id INNER JOIN "profiles" ON "profiles".id = "survey_sessions".profile_id WHERE ("profiles"."性别" = E'm') 按个人资料分组。职业

这有点难以阅读,但说我将表配置文件包含了两次。

如果我只是从平均值中删除包含,它可以工作,但实际上并不实用,因为平均值实际上是在传递作用域的方法内调用的。因此,有时性别或平均水平可能会在没有对方的情况下被调用,如果其中一个缺少个人资料,则它将不起作用。

因此,我要么需要知道如何修复 Rails 中的这个明显错误,要么找出一种方法来了解 ActiveRecord::NamedScope::Scope 对象应用了哪些范围,以便我可以检查它们是否已应用,如果没有应用添加平均值的包含。

I have a named scopes like so...

  named_scope :gender, lambda { |gender| { :joins => {:survey_session => :profile }, :conditions => { :survey_sessions => { :profiles => { :gender => gender } } } } }

and when I call it everything works fine.

I also have this average method I call...

Answer.average(:rating, :include => {:survey_session => :profile}, :group => "profiles.career")

which also works fine if I call it like that.

However if I were to call it like so...

Answer.gender('m').average(:rating, :include => {:survey_session => :profile}, :group => "profiles.career")

I get...

ActiveRecord::StatementInvalid: PGError: ERROR: table name "profiles" specified more than once
: SELECT avg("answers".rating) AS avg_rating, profiles.career AS profiles_career FROM "answers" LEFT OUTER JOIN "survey_sessions" survey_sessions_answers ON "survey_sessions_answers".id = "answers".survey_session_id LEFT OUTER JOIN "profiles" ON "profiles".id = "survey_sessions_answers".profile_id INNER JOIN "survey_sessions" ON "survey_sessions".id = "answers".survey_session_id INNER JOIN "profiles" ON "profiles".id = "survey_sessions".profile_id WHERE ("profiles"."gender" = E'm') GROUP BY profiles.career

Which is a little hard to read but says I'm including the table profiles twice.

If I were to just remove the include from average it works but it isn't really practical because average is actually being called inside a method which gets passed the scoped. So there is some times gender or average might get called with out each other and if either was missing the profile include it wouldn't work.

So either I need to know how to fix this apparent bug in Rails or figure out a way to know what scopes were applied to a ActiveRecord::NamedScope::Scope object so that I could check to see if they have been applied and if not add the include for average.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

终陌 2024-09-08 14:43:32

看起来 ActiveRecord 正在生成一些错误的 SQL:

SELECT avg("answers".rating) AS avg_rating,
       profiles.career AS profiles_career
FROM "answers"
     LEFT OUTER JOIN "survey_sessions" survey_sessions_answers
                    ON "survey_sessions_answers".id = "answers".survey_session_id
     LEFT OUTER JOIN "profiles"
                     ON "profiles".id = "survey_sessions_answers".profile_id
     INNER JOIN "survey_sessions"
                ON "survey_sessions".id = "answers".survey_session_id
     INNER JOIN "profiles"
                ON "profiles".id = "survey_sessions".profile_id
WHERE ("profiles"."gender" = E'm')
GROUP BY profiles.career

大概它生成了左联接作为获取投影属性的一部分,以及内联接作为获取条件的一部分:如果它为这些分配了别名,则这不会是无效的(只是效率低下)表,但事实并非如此。有没有办法从您的应用程序指定别名?

Looks like ActiveRecord is generating some bad SQL:

SELECT avg("answers".rating) AS avg_rating,
       profiles.career AS profiles_career
FROM "answers"
     LEFT OUTER JOIN "survey_sessions" survey_sessions_answers
                    ON "survey_sessions_answers".id = "answers".survey_session_id
     LEFT OUTER JOIN "profiles"
                     ON "profiles".id = "survey_sessions_answers".profile_id
     INNER JOIN "survey_sessions"
                ON "survey_sessions".id = "answers".survey_session_id
     INNER JOIN "profiles"
                ON "profiles".id = "survey_sessions".profile_id
WHERE ("profiles"."gender" = E'm')
GROUP BY profiles.career

Presumably it's generated the left joins as part of getting the projected property, and the inner joins as part of getting the criteria: this wouldn't be invalid (just inefficient) if it assigned aliases to those tables, but it doesn't. Is there a way to specify an alias name from your app?

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