2017-11-06 12 views
2

created_at列でEctoクエリを注文すると、次のコードが正常に動作します。私は情報と呼ばれるタイプjsonbのデータベース列があり、JSONオブジェクト内のフィールドの一つが、「ステータス」で、そのフィールドでソートする方法があると仮定すると、Ectoクエリ - jsonb列のorder_byフィールド

sort_params = [asc: :created_at] 
 
query = from user in users, 
 
     order_by: ^sort_params, 
 
     limit: ^page_size, 
 
     offset: ^offset

? んエクトは

sort_params = [asc: :info.status]

答えて

2

はい、

あなたはそのためのEcto.Query.API.fragment/1を使用することができるようなものへの有効な同等を持っています。

# you can use fragment/1 only directly in the query 

query = from user in users, 
    order_by: fragment("info->>'status' ASC"), 
    limit: ^page_size, 
    offset: ^offset 
関連する問題