简体   繁体   English

如何使用where子句查询rails中的jsob列

[英]How to query jsob column in rails using where clause

I'm trying to fetch the record which the fields are not null.我正在尝试获取字段不是 null 的记录。

for example I'm having a jsonb column in my table like this例如,我的表中有一个 jsonb 列,如下所示

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.我想获取 key enqued_at 的 job_metadata hash 的记录,这些记录不是 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?如何修复错误,或者如何获取 job_metadata -> enqueued_at 不应该为 nil 的记录?

This should work.这应该有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM