加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails ActiveRecord Model.uniq.pluck:id不适

发布时间:2020-12-17 02:38:54 所属栏目:百科 来源:网络整理
导读:我有一个空的demo rails 4 app尝试执行以下操作:Collection.order(‘created_at ASC’).uniq.pluck:name它在sqlite下工作,但在postgres中爆炸,出现以下错误: (0.9ms) SELECT DISTINCT "collections"."name" FROM "collections" ORDER BY created_at ASCPG
我有一个空的demo rails 4 app尝试执行以下操作:Collection.order(‘created_at ASC’).uniq.pluck:name它在sqlite下工作,但在postgres中爆炸,出现以下错误:

(0.9ms)  SELECT DISTINCT "collections"."name" FROM "collections" ORDER BY created_at ASC
PG::Error: ERROR:  for SELECT DISTINCT,ORDER BY expressions must appear in select list
LINE 1: ...collections"."name" FROM "collections"   ORDER BY created_at...

这是一个错误或我该如何解决?

解决方法

这似乎是PostgreSQL的一个问题. ( Rails 3 DISTINCT QUERY)

要解决它,您可以使用select代替:

Collection.select([:name,:created_at]).order(‘created_at ASC’).uniq.select(:name)

或者你可以让Ruby获得唯一的名称而不是SQL:

Collection.order(‘created_at ASC’).pluck(:name).uniq

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读