ruby on rails - Sort by date in jsonb postgres -


i have model data stored in json format in jsonb column in postgres.

i want sort output data field using activerecord query.

model.all.order("json_data -> 'date'") 

gives me output orders alphabetically based on date string.

is there easy way can sort date?

note: dates in following format:

"fri, 24 jun 2016 04:13:26 -0700" 

if date in sensible format postgres deal automatically.

model.all.order("(json_data ->> 'date')::timestamp time zone desc") 

or

model.all.order("(json_data ->> 'date')::timestamptz desc") 

if date field string little unorthodox, can following

model.all.order("to_timestamp(json_data->>'date','dy, dd mon yyyy hh24:mi:ss ') desc") 

details here

note ->> there output string rather json object.

you can of course create column , store information there per @uzbekjon's answer below.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -