我有一个模型任务

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 这是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.


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

最佳答案

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")

关于ruby-on-rails - 返回没有特定键的记录-Rails4 HStore Postgresql查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17645329/

10-09 14:05