Rails Postgres-Jsonb列查询

时间:2020-05-28 13:16:41

标签: ruby-on-rails ruby postgresql

我有一个名为“ available_quantity”的jsonb列的Bundle表格。将会有样本值

[{“面额”:“ 100”,“数量”:“ 20”},{“面额”:“ 1000”,“数量”:“ 19”}]

现在,我要查询数量少于50的所有记录。

我尝试了此查询,

Bundle.where(“(available_quantity->>'quantity'):: numeric <50”)

但这返回空的Relation。

我该怎么做?

SCHEMA

                                            Table "offer_service.bundles"
    Column         |            Type             | Collation | 
Nullable |                        Default                         
-----------------------+-----------------------------+-----------+---- 
------+--------------------------------------------------------
id                    | bigint                      |           | not 
null | nextval('offer_service.bundles_id_seq'::regclass)
project_id            | bigint                      |           |          
| 
 item_type             | character varying           |           |          
| 
item_id               | bigint                      |           |          
| 
status                | integer                     |           | not 
 null | 0
 created_at            | timestamp without time zone |           | not 
null | 
 updated_at            | timestamp without time zone |           | not 
null | 
denomination_quantity | jsonb                       |           |          
| 
 deleted_at            | timestamp without time zone |           |          
| 
 available_quantity    | jsonb                       |           |          
| 

示例数据:

id: 2586, project_id: 3, item_type: "GiftCard", item_id: 659, status: 
"activated", created_at: "2020-05-18 09:38:54", updated_at: "2020-05- 
28 13:25:29", denomination_quantity: {"100"=>200, "1000"=>200}, 
deleted_at: nil, available_quantity: [{"quantity"=>16, 
"denomination"=>"100"}, {"quantity"=>20, "denomination"=>1000}]

1 个答案:

答案 0 :(得分:1)

希望我们必须在这里使用子查询。

java HelloWorld

欢迎重构以上查询的建议!