Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find values greater than using hstore and rails

I am storing product information, including the release year of the product, using hstore and postgresql in rails. Now I would like to be able to query for all products that was released before or after a specific year. I am able to query for all records containing the year field in the 'data' hstore column using:

Product.where("data ? 'year'")

Due to hstore the year value is stored as a string. Therefore, I have tried to type cast the year to an integer in order to find records with years greater than/less than X:

Product.where("(data ? 'year')::int > 2011")

However, this does not seem to work, I always get an empty array of results in return. What am I doing wrong? Is there another way to do this?

like image 602
graphmeter Avatar asked Nov 02 '22 13:11

graphmeter


1 Answers

I think the operator your are looking for is ->.

So, try this : where("(data -> 'year')::int > 2011")

(from jO3w's comment)

like image 76
charlysisto Avatar answered Nov 13 '22 22:11

charlysisto