返回没有特定键的记录 - Rails4 HStore Postgresql查询

时间:2013-07-15 00:56:59

标签: ruby-on-rails postgresql-9.1 hstore

我有一个模型任务

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&lt; 10和key:今天不存在。 这是1,2,3任务。

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.

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

1 个答案:

答案 0 :(得分:8)

来自here,根据您的问题:

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

根据你的意见:

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