How can I make a request using a JSON function / operator where the key is either non-null or null?

I am looking for the active record version of the following sql code:

select *
from users
where (details->'email') is not null

      

Let's say my user model has a json field, details and some records will have an email key value and some will not. I am trying to query user records that have email for a key value. I tried using the following User.where ("details ->" email "not null"), but my terminal prints this out:

PG::UndefinedFunction: ERROR:  operator does not exist: json -> boolean

      

I found this stackoverflow post that tries the opposite request, but the idea is the same. If someone can show me the Active Record version of the request, whether or not there is a json key, I would really appreciate it.

+3


source to share


1 answer


This worked for users to get their email key:

User.where("(details->'email') is not null")

      



This worked for getting users without an email key:

User.where("(details->'email') is null")

      

+5


source







All Articles