如何从序列化对象中跳过关系哈希

时间:2019-02-20 06:47:41

标签: ruby-on-rails ruby

如何跳过序列化对象(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"
        }
      }
    }
  }
}

1 个答案:

答案 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=>""}}}