Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to check if jsonb column is empty Postgres

I am having trouble checking when the jsonb column of my table is empty.

My column directions when empty has value "{}"

Tried the following

Model.where("directions != '{}'") <- brings all
Model.where("directions <@ '{}'") <- brings all

is there any other way that i am not aware of? Using postgresql 9.6

like image 917
Petros Kyriakou Avatar asked Nov 09 '22 06:11

Petros Kyriakou


2 Answers

Model.where.not(directions: '{}')
like image 51
Deepak Mahakale Avatar answered Nov 15 '22 05:11

Deepak Mahakale


Model.where.not("directions::text = ?", "{}")
like image 28
Fran Martinez Avatar answered Nov 15 '22 05:11

Fran Martinez