我需要使用active_model_serializers gem创建一个类似的结构
let wishlist = ["444 Winter St, Indoor Pool",
"275 Meadowbrook Rd, Outdoor Pool",
"Someplace in Wellesley, Both Infoor and Outdoor Pools",
"a place in Natick, Either pools"]
类别是指PORO病毒:
{
"categories": [
{
"code": "code",
"name": "name"
},
{
"code": "code",
"name": "name"
},
{
"code": "code",
"name": "name"
},
{
"code": "code",
"name": "name"
}
]
}
我已经对该序列化器进行了编码,以尝试创建该哈希。它奏效了,但是结果却出乎我的意料。
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
结果
module Api
module Bitsky
class MarketStructure< ActiveModel::Serializer
attributes :categories
def categories
*** some operation ***
end
end
end
end
我如何编写一个序列化程序,该序列化程序仅返回没有此键“类别”的哈希数组?
答案 0 :(得分:2)
您应该做这样的事情
@categories.as_json(root: false)
或
render json: @categories, root: false