Petros Kyriakou
Petros Kyriakou

Reputation: 5343

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

Upvotes: 7

Views: 4595

Answers (3)

Fran Martinez
Fran Martinez

Reputation: 3042

Model.where.not("directions::text = ?", "{}")

Upvotes: 3

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

Model.where.not(directions: '{}')

Upvotes: 3

yeasayer
yeasayer

Reputation: 868

Try to negate query with .not:

Model.where.not("directions = '{}'")

Upvotes: -3

Related Questions