ruby-on-rails - 如何在 Rails 4 中通过 hstore 属性对结果进行排序?

标签 ruby-on-rails ruby postgresql hstore

如何通过 hstore 属性对查询结果进行排序?

@items = Item.includes(:product).order('products.properties @> hstore("platform")')

原因

PG::Error: ERROR:  column "platform" does not exist
LINE 1: ...oduct_id"  ORDER BY products.properties @> hstore("platform"...

platform是一个hstore键,存放在properties列中,是一个hstore类型。

最佳答案

双引号用于在 PostgreSQL(以及其他遵循该标准的数据库)中引用标识符(例如表名和列名)。所以当你说:

hstore("platform")

PostgreSQL 将 "platform" 视为带引号的列名称,并且由于没有 platform 列,因此您会收到错误消息。

标准SQL中的字符串用单引号引起来,你想说:

.order("products.properties @> hstore('platform')")

这可能仍然会失败,hstore('platform') 没有多大意义,在这里使用 @> 也没有意义; a @> b意味着

does the hstore a contain the hstore b

如果您尝试对 properties hstore 中 'platform' 键的值进行排序,那么您需要使用 -> 像这样查找 'platform' 键:

.order("products.properties -> 'platform'")

关于ruby-on-rails - 如何在 Rails 4 中通过 hstore 属性对结果进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16284948/

相关文章:

ruby-on-rails - 在 Rails 应用程序上使用 Prawn gem 重复页脚与当前页面和每页的总页数,但重叠当前页码

jquery - will_paginate 使用 jQuery 在 Rails 3.1 中加载更多按钮

ruby - 为什么 Rubymine 建议我在字符串上使用单引号而不是双引号?

ruby-on-rails - #<String :0x00000003a27a58> 的未定义方法 `each'

ruby-on-rails - EmberJS 使用 ember-ajax 向 Rails API 服务器发出 HTTP Post 请求

ruby - 在 Ruby 中,tLABEL 到底是什么意思?

ruby - 强参数 : Unpermitted parameters: tags in has_many_through relation

python - 将 PostgreSQL 整数转换为日期

java - 使用 Postgres 和 Hibernate(Grails) 选择语句后在事务中空闲

postgresql - Postgres:为什么选择count(*)需要这么长时间