从Active Model Serialized对象中的数组中删除空哈希{}

时间:2018-09-04 11:54:48

标签: ruby-on-rails active-model-serializers activemodel

在某些情况下,我不想序列化当前对象并希望跳过它。但是我还没有找到一种方法来做到这一点,所以我忽略了属性:foo,if::condition的属性。这会在数组内的序列化对象中生成空{}。我该如何解决?

[
 {
  "id": 392027,
  "name": "ISC Board",
  "grades":[
            {
                "id": 333938,
                "name": "1",
                "subjects": [
                    {
                        "id": 571671,
                        "subject": "Math"
                    },
                    {
                        "id": 742980,
                        "subject": "Science"
                    },
                    {
                        "id": 186926,
                        "subject": "English"
                    },
                    {
                        "id": 658224,
                        "subject": "Social_Studies"
                    },
                    {},
                    {},
                    {}
                ]
            },
            {
                "id": 333943,
                "name": "2",
                "subjects": [
                    {
                        "id": 571671,
                        "subject": "Math"
                    },
                    {
                        "id": 742980,
                        "subject": "Science"
                    },
                    {
                        "id": 186926,
                        "subject": "English"
                    },
                    {
                        "id": 658224,
                        "subject": "Social_Studies"
                    },
                    {},
                    {},
                    {}
                ]
            },
          ]
 },
 {
  "id": 666627,
  "name": "NY Board",
  "grades":[
            {
                "id": 333938,
                "name": "1",
                "subjects": [
                    {
                        "id": 571671,
                        "subject": "Math"
                    },
                    {
                        "id": 742980,
                        "subject": "Science"
                    },
                    {
                        "id": 186926,
                        "subject": "English"
                    },
                    {
                        "id": 658224,
                        "subject": "Social_Studies"
                    },
                    {},
                    {},
                    {}
                ]
            },
            {
                "id": 432943,
                "name": "2",
                "subjects": [
                    {
                        "id": 571671,
                        "subject": "Math"
                    },
                    {
                        "id": 742980,
                        "subject": "Science"
                    },
                    {
                        "id": 186926,
                        "subject": "English"
                    },
                    {
                        "id": 658224,
                        "subject": "Social_Studies"
                    },
                    {},
                    {},
                    {}
                ]
            },
          ]
  }


]

序列化器看起来像这样-

class BoardSerializer < ActiveModel::Serializer
  #some code
  class GradeSerializer < ActiveModel::Serializer 
     has_many :subjects
     #some code
     class SubjectSerializer < ActiveModel::Serializer
        attribute :id, if: :condition
        attribute :name, key: :subject, if: :condition

        def condition
            #some code
            #returns true or false
            #will not return both :id and :subject if false- I want to 
            #skip this current object if condition fails. (returns {})
        end  
     end

   end
end

如何简单地跳过序列化程序中的当前对象或删除空哈希?谢谢

2 个答案:

答案 0 :(得分:1)

请检查这是否是预期结果:

input.transform_values { |v| v.map {|e| e.transform_values { |vv| vv.class == Array ? vv.select { |ee| ee unless ee.empty? } : vv } } }

# => {:grades=>[{:id=>333938, :name=>"1", :subjects=>[{:id=>571671, :subject=>"Math"}, {:id=>742980, :subject=>"Science"}, {:id=>186926, :subject=>"English"}, {:id=>658224, :subject=>"Social_Studies"}]}]}


编辑:以满足OP问题中的更改。

input.map { |e| e.transform_values { |v| v.is_a?(Array) ? v.map {|ee| ee.transform_values { |vv| vv.is_a?(Array) ? vv.select { |eee| eee unless eee.empty? } : vv } } : v } }

# => [{:id=>392027, :name=>"ISC Board", :grades=>[{:id=>333938, :name=>"1", :subjects=>[{:id=>571671, :subject=>"Math"}, {:id=>742980, :subject=>"Science"}, {:id=>186926, :subject=>"English"}, {:id=>658224, :subject=>"Social_Studies"}]}, {:id=>333943, :name=>"2", :subjects=>[{:id=>571671, :subject=>"Math"}, {:id=>742980, :subject=>"Science"}, {:id=>186926, :subject=>"English"}, {:id=>658224, :subject=>"Social_Studies"}]}]}, {:id=>666627, :name=>"NY Board", :grades=>[{:id=>333938, :name=>"1", :subjects=>[{:id=>571671, :subject=>"Math"}, {:id=>742980, :subject=>"Science"}, {:id=>186926, :subject=>"English"}, {:id=>658224, :subject=>"Social_Studies"}]}, {:id=>432943, :name=>"2", :subjects=>[{:id=>571671, :subject=>"Math"}, {:id=>742980, :subject=>"Science"}, {:id=>186926, :subject=>"English"}, {:id=>658224, :subject=>"Social_Studies"}]}]}]

答案 1 :(得分:0)

您可以使用input = { "grades": [ { "id": 333938, "name": "1", "subjects": [ { "id": 571671, "subject": "Math" }, { "id": 742980, "subject": "Science" }, { "id": 186926, "subject": "English" }, { "id": 658224, "subject": "Social_Studies" }, {}, {}, {} ] } ] } input[:grades].first[:subjects].select! { |i| !i.empty? } 来解决此问题:

delete