0

I need to create a structure like that using the active_model_serializers gem

{
  "categories": [
    {
      "code": "code",
      "name": "name"
    },
    {
      "code": "code",
      "name": "name"
    },
    {
      "code": "code",
      "name": "name"
    },
    {
      "code": "code",
      "name": "name"
    }
  ]
}

Categories refers to a PORO virtus:

module Api
  module Bitsky
    class MarketStructure
      include Virtus.model

      attribute :department_name, String
      attribute :department_code, Integer
      attribute :department_value, String
      attribute :sector_name, String
      attribute :sector_code, Integer
      attribute :sector_value, String
      attribute :family_name, String
      attribute :family_code, Integer
      attribute :family_value, String
      attribute :sub_family_name, String
      attribute :sub_family_code, Integer
      attribute :sub_family_value, String
    end
  end
end

I've code this serializer to try to create that hash. It worked, however the result was not what I expected.

module Api
  module Bitsky
    class MarketStructure< ActiveModel::Serializer
      attributes :categories

     def categories
       *** some operation ***
     end
    end
  end
end

Result

{
  "categories": {
    "categories": [
      {
        "code": "code",
        "name": "name"
      }
    ]
  }
}

How do I write a serializer which return only a Hash Array without this key "categories" ?

1 Answer 1

3

You should be doing something like this

@categories.as_json(root: false)

or

render json: @categories, root: false
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.