7

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

2
  • Model.where("directions != '{}'") should work, but Model.where.not(directions: '{}') feels more railsy (but is equivalent). If these queries return all of your data, then are you sure you have any instances with actual directions data in them? Commented Nov 4, 2016 at 12:23
  • @Frost the only syntax that worked is Model.where.not(directions: '{}') . To answer your questions yes i had both-case records. Commented Nov 4, 2016 at 13:42

3 Answers 3

4
Model.where.not("directions::text = ?", "{}")
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

ERROR: operator does not exist: json <> unknown
@philpirozhkov gotta check if something changed during these 4-5 years :)
This didn't work for me unless I removed the quotes: where.not(directions: {}) (I'm using Rails 6.1.7 and Postgres 16)
Good to know. I will check if anything is changed in last 8 years :+1
-3

Try to negate query with .not:

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

1 Comment

I agree with Petros, this is invalid syntax as of 2017 and Rails 4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.