Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 732
Cameron Barker Avatar asked Oct 24 '25 14:10

Cameron Barker


1 Answers

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")

like image 92
jcuenod Avatar answered Oct 26 '25 05:10

jcuenod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!