[英]How do I order results by hstore attribute in Rails 4?
How can I order query results by an hstore attribute? 如何通过hstore属性订购查询结果?
@items = Item.includes(:product).order('products.properties @> hstore("platform")')
Causes 原因
PG::Error: ERROR: column "platform" does not exist
LINE 1: ...oduct_id" ORDER BY products.properties @> hstore("platform"...
platform
is a hstore key, stored in the properties column, which is an hstore type. platform
是一个hstore密钥,存储在属性列中,这是一个hstore类型。
Double quotes are used to quote identifiers (such as table and column names) in PostgreSQL (and other databases that follow the standard). 双引号用于引用PostgreSQL(以及遵循该标准的其他数据库)中的标识符(例如表名和列名)。 So when you say:
所以当你说:
hstore("platform")
PostgreSQL sees "platform"
as a quoted column name and since there is no platform
column, you get an error. PostgreSQL将
"platform"
视为引用的列名,由于没有platform
列,因此会出现错误。
Strings in standard SQL are quoted with single quotes, you want to say: 标准SQL中的字符串引用单引号,您想说:
.order("products.properties @> hstore('platform')")
This will probably still fail though, hstore('platform')
doesn't make much sense and neither does using @>
here; 这可能仍然会失败,但是
hstore('platform')
没有多大意义,也没有在这里使用@>
; a @> b
means a @> b
表示
does the hstore
a
contain the hstoreb
确实的hstore
a
包含hstoreb
If you're trying to sort on the value of the 'platform'
key in the properties
hstore then you'd want to use ->
to lookup the 'platform'
key like this: 如果您尝试在
properties
hstore中对'platform'
键的值进行排序,那么您需要使用->
来查找'platform'
键,如下所示:
.order("products.properties -> 'platform'")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.