Home > other >  How to query jsob column in rails using where clause
How to query jsob column in rails using where clause

Time:09-05

I'm trying to fetch the record which the fields are not null.

for example I'm having a jsonb column in my table like this

job_metadata jsonb DEFAULT '{}'::jsonb NOT NULL

i want to fetch the record of job_metadata hash of key enqued_at, which are not null.

i tried something like

TimeBox.where.not('job_metadata @> ?', '{"enqueued_at": nil}')

but i got the error like

PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type json
LINE 1: ... 38, 39, 40, 51, 52, 53, 32) AND (job_metadata @> '{"enqueued_at...

how to fix the error, or how to fetch the records where the job_metadata -> enqueued_at should not be nil?

CodePudding user response:

This should work.

TimeBox.where("(job_metadata->'enqueued_at') is not null")
  • Related