合并ActiveModel序列化器结果

时间:2019-05-29 19:35:53

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

我有以下ActiveModel序列化程序,该序列化程序分析以下数据:

class CarSerializer < ActiveModel::Serializer
attributes :brand, :wheels, :events
def brand
  { id: object.id, brand_name: object.brand_name }
end

def wheels
  object.wheel_details
end

def events
  object.car_events
end
end

,以下是显示的数据:

{
"car_info": [
{
"car": {
"id": 1,
"brand_name": "opel"
},
"wheels": [
{
"id": 2,
"size": "23.4"
},
{
"id": 1,
"size": "22.3"
}
],
"events": [
{
"id": 1,
"event_place": "america"
},
{
"id": 1,
"event_place": "asia"
}
]

但是,我希望数据看起来像这样:

{
"car_info": [
{
"car": {
"id": 1,
"brand_name": "opel"
},
"wheels_and_events": [
{
"id": 2,
"size": "23.4"
"event_place": "america"
},
{
"id": 1,
"size": "22.3"
"event_place": "asia"
}
]

我通过3个表的关联和一个表的关联来使用has_many。

0 个答案:

没有答案