简体   繁体   中英

Ordering Postgres jsonb in Rails

I'm trying to order by a jsonb column in rails using:

Stat.order("data ->'likes'->'followed_by' ASC")

I keep getting this error returned.

PG::UndefinedFunction: ERROR:  could not identify an ordering operator for type json

I can't figure out is it my formatting? The followed_by attribute is a int.

Thank you!

I'm not certain but according to http://www.postgresql.org/docs/9.3/static/functions-json.html

  1. -> "Get JSON object field"
  2. ->> "Get JSON object field as text"

So perhaps activerecord can't sort the field but can sort the text ?

Try: Stat.order("data ->'likes'->>'followed_by' ASC")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM