我正在寻找以下sql代码的事件记录版本:

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

假设我的用户模型有一个json字段,称为details,有些记录将具有电子邮件键值,而有些则没有。我正在尝试查询具有电子邮件键值的用户记录。我尝试使用以下User.where(“details->'email'不为null”),但是我的终端将输出以下内容:
PG::UndefinedFunction: ERROR:  operator does not exist: json -> boolean

我发现了这个SO帖子,它尝试执行相反的查询,但是想法是相同的。如果有人可以向我显示Active Record版本的json键是否存在的查询,我将不胜感激。

最佳答案

这可以使用户获得电子邮件 key :

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

这可以使用户没有电子邮件 key :
User.where("(details->'email') is null")

10-01 04:55
查看更多