查找大于使用hstore和rails的值

gra*_*ter 5 ruby ruby-on-rails pg rails-postgresql hstore

我正在使用rails中的hstore和postgresql存储产品信息,包括产品的发布年份.现在,我希望能够查询在特定年份之前或之后发布的所有产品.我可以使用以下方法查询'data'hstore列中包含year字段的所有记录:

Product.where("data ? 'year'")
Run Code Online (Sandbox Code Playgroud)

由于hstore,年份值存储为字符串.因此,我尝试将年份类型转换为整数,以便查找年份大于/小于X的记录:

Product.where("(data ? 'year')::int > 2011")
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不起作用,我总是得到一个空数组的结果作为回报.我究竟做错了什么?还有另一种方法吗?

cha*_*sto 2

我认为您正在寻找的运营商是->

所以,试试这个:where("(data -> 'year')::int > 2011")

(来自jO3w的评论)