小编use*_*650的帖子

Rails将JSON解析为哈希

我尝试解析JSON响应.

@response = ActiveSupport::JSON.decode(@data.body)
Run Code Online (Sandbox Code Playgroud)

@响应:

{"response"=>[{"name"=>"Den", "other"=>[{"sex"=>"male"}]}]}
Run Code Online (Sandbox Code Playgroud)

如何访问

@response['response']['name'] 
Run Code Online (Sandbox Code Playgroud)

@response['response']['name']['sex']
Run Code Online (Sandbox Code Playgroud)

parsing json ruby-on-rails

3
推荐指数
1
解决办法
7696
查看次数

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

我有一个模型任务

t.string   "name"
t.hstore   "actions"
Run Code Online (Sandbox Code Playgroud)

例:

#<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"},
Run Code Online (Sandbox Code Playgroud)

我需要找到行动的所有记录:nil,:今天<10和关键:今天不存在.这是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.
Run Code Online (Sandbox Code Playgroud)

我怎么能找到所有没有钥匙的记录:今天在行动?

ruby-on-rails postgresql-9.1 hstore

2
推荐指数
1
解决办法
1511
查看次数

标签 统计

ruby-on-rails ×2

hstore ×1

json ×1

parsing ×1

postgresql-9.1 ×1