如何跳过序列化对象(gem Netflix / fast_jsonapi)中的关系哈希?
{
"data": {
"id": "3",
"type": "movie",
"attributes": {
"name": "test movie",
"year": null
},
"relationships": {
"actors": {
"data": [
{
"id": "1",
"type": "actor"
},
{
"id": "2",
"type": "actor"
}
]
},
"owner": {
"data": {
"id": "3",
"type": "user"
}
}
}
}
}
答案 0 :(得分:0)
在可以使用的情况下,可以使用rails除了方法从哈希中删除属性
data_hash = {"data": {"id": "3","type": "movie","attributes": {"name": "test movie","year":''},"relationships": {"actors": {"data": [{"id": "1","type": "actor"},{"id": "2","type": "actor"}]},"owner": {"data": {"id": "3","type": "user"}}}}}
data_hash[:data].except!(:relationships)
您将得到如下输出
{:id=>"3", :type=>"movie", :attributes=>{:name=>"test movie", :year=>""}}
data_hash
将
{:data=>{:id=>"3", :type=>"movie", :attributes=>{:name=>"test movie", :year=>""}}}