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?
I think the operator your are looking for is ->
.
So, try this : where("(data -> 'year')::int > 2011")
(from jO3w's comment)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With