返回没有特定键的记录-Rails4 HStore Postgresql查询 [英] Return records that don't have specific key - Rails4 HStore Postgresql query

查看:80
本文介绍了返回没有特定键的记录-Rails4 HStore Postgresql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务模型

t.string   "name"
t.hstore   "actions"

示例:

#<Task id: 1, name: "first", actions: {"today"=>"9"}, 
#<Task id: 2, name: "second", actions: {"yesterday"=>"1"}, 
#<Task id: 3, name: "third", actions: nil,
#<Task id: 4, name: "four", actions: {"today"=>"11"},

我需要找到动作不存在的所有记录:nil,:today< 10和key:today不存在。
这是1,2,3个任务。

I need to find all records where actions: nil, :today<10 and key :today not present. It is 1,2,3 task.

Task.where("actions -> 'today' < '10'") - it return only first task.
Task.where("actions -> 'today' < '10' OR actions IS NULL")  - it return 1 and 3 records.

我如何找到所有没有键的记录:今天在行动?

How i can find all records which do not have the key :today in action?

推荐答案

来自此处,根据您的问题:

Task.where("actions IS NULL OR EXIST(actions, 'today') = FALSE")

并根据您的评论:

Task.where("actions -> 'today' < '10' OR actions IS NULL OR EXIST(actions, 'today') = FALSE")

这篇关于返回没有特定键的记录-Rails4 HStore Postgresql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆