当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - Order by number in Postgres JSONB column

问题描述:

I have a position key defined in my JSONB column.

The values are treated as text, so the following query

MyModel.order("data ->> 'position' ASC").each {|x| puts x.position}

returns:

0

1

10

2

3

How can I treat position as integer and order my model based on that?

网友答案:

Purely a guess, but maybe:

MyModel.order("(data ->> 'position')::Integer ASC").each {|x| puts x.position}
分享给朋友:
您可能感兴趣的文章:
随机阅读: