【问题标题】:Postgresql Arel and Group ByPostgresql Arel 和分组依据
【发布时间】:2012-12-17 23:14:26
【问题描述】:

我在本地使用 arel 创建了我自己的搜索。

def self.search(search)
    User.joins(:experience).where(Experience.arel_table[:description].matches("%#{search}%")
            .or(Experience.arel_table[:description].matches("%#{search.capitalize}%"))
            .or(Experience.arel_table[:job_title].matches("%#{search}%"))
            .or(Experience.arel_table[:job_title].matches("%#{search.capitalize}%")))
            .group(:user_id)
end

一切都很好,直到推向heroku。

Heroku 日志向我显示以下消息:

ActionView::Template::Error (PGError: ERROR:  column "users.id" must appear in the GROUP BY clause or be used in an aggregate function

这是选择:

SELECT  "users".* FROM "users" INNER JOIN "experiences" ON "experiences"."user_id" = "users"."id" WHERE (((("experiences"."description" ILIKE '%rails%' OR "experiences"."description" ILIKE '%Rails%') OR "experiences"."job_title" ILIKE '%rails%') OR "experiences"."job_title" ILIKE '%Rails%')) GROUP BY user_id ORDER BY created_at DESC LIMIT 7 OFFSET 0):

如你所见,我的方法中有 .group(:user_id),所以我不理解这个错误

提前感谢您的帮助。

更新

在我改变这样的方法之后:

User.joins(:experience).where(Experience.arel_table[:description].matches("%#{search}%")
.or(Experience.arel_table[:description].matches("%#{search.capitalize}%"))
.or(Experience.arel_table[:job_title].matches("%#{search}%"))
.or(Experience.arel_table[:job_title].matches("%#{search.capitalize}%")))
.select("experiences.user_id, users.email")
.group("experiences.user_id, users.email")

在我看来这个错误

undefined method `name' for nil:NilClass 

在这一行

<%= user.information.name %>

如果我删除此行,我会收到此错误

Routing Error

No route matches {:action=>"show", :controller=>"users", :id=>#<User email: "jgiron@hotmail.com">}

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 heroku ruby-on-rails-3.2


    【解决方案1】:

    你需要做一个select

    User.joins(:experience).where(Experience.arel_table[:description].matches("%#{search}%")
            .or(Experience.arel_table[:description].matches("%#{search.capitalize}%"))
            .or(Experience.arel_table[:job_title].matches("%#{search}%"))
            .or(Experience.arel_table[:job_title].matches("%#{search.capitalize}%")))
            .select("experiences.user_id, users.email")
            .group("experiences.user_id, users.email")
    

    如果您不指定要选择的列,Rails 默认会尝试选择所有列。

    更新

    在进行 SQL 查询时,所有出现在 SELECT 中的字段都必须出现在 GROUP BY 中。所以如果你有SELECT foo, bar, baz, MAX(id) FROM foo_bars GROUP BY foo, bar, baz,那么foobarbaz必须在GROUP BY中。由于MAX(id) 是一个聚合函数,它不必出现在GROUP BY 中。

    然而,如果你不想做这种GROUP BY,那么Rails 有一个名为group_by 的应用程序端方法。你可以这样使用它:

    @experiences.group_by {|x| x.user_id} 
    

    这将返回一个哈希值,其中键作为用户 ID,值作为体验。

    【讨论】:

    • 什么不起作用?您将需要选择所有需要的字段,就像普通的 SQL 查询一样。 .select("user_id, email").group(:user_id, :email)
    • 这是错误 SQLite3::SQLException: ambiguous column name: created_at: SELECT user_id, email FROM "users" INNER JOIN "experiences" ON "experiences"."user_id" = "users"。 id" WHERE ((((("experiences"."description" LIKE '%perra%' OR "experiences"."description" LIKE '%Perra%') OR "experiences"."job_title" LIKE '%perra%') OR "experiences"."job_title" LIKE '%Perra%')) GROUP BY user_id, email ORDER BY created_at DESC LIMIT 7 OFFSET 0
    • 在这种情况下,您将不得不这样做.select("experiences.user_id, users.email").group("experiences.user_id, users.email")
    • 谢谢肖恩,但我得到了同样的错误,但现在有了这个列
    • 和什么一样的错误?模棱两可的列错误?这是不可能的。
    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多