Reputation: 13872
I have a model Post
#<Post id: 121978, created_at: "2014-05-02 18:11:15", updated_at: "2014-05-02 18:11:15", data: {"hi"=>"1", "hello"=>"9999"}, review_id: nil>
And I'd like to sort them based on hello
within the hstore datatype column data
I made this query:
Post.order("data -> 'hello'")
Which works but since Hstore is in string, I have 780, 78, 77, ...
as an output.
Upvotes: 0
Views: 457
Reputation: 821
I think you can cast the value as an integer before being processed by order. I'm not that familiar with Hstore, but here are some examples of how you might try to do that:
Post.order("CAST(data -> 'hello' AS INT)")
Post.order("CONVERT(INT, data -> 'hello')")
Post.order("(data -> 'hello') * 1")
Upvotes: 4