ruby-on-rails ROR:序列化多个对象的输出

ao218c7q  于 2023-02-06  发布在  Ruby
关注(0)|答案(1)|浏览(158)

我正在学习rails,并且正在使用active_model_serializers gem。
我需要从两个不同的表中获取JSON格式的定制输出。
汽车:ID,名称,颜色,大小,形状,日期...电池:id、car_id、价格、重量、姓名、日期......

class battery < ApplicationRecord
  belongs_to :car
end

我运行的查询是

## This is sample query will not work
Car.includes(:battery).where(car: {color: red}).where(date >= 'blah')

我在控制器中计算了三个哈希值

#cars_controller.rb
available_cars ={ 
                   available_cars:[{id:id, 
                                    name:name, 
                                    color:color, 
                                    size:size, 
                                    date:date..
                                   }] 
                  }

available_battries = {
                      available_batteries:[{id:id, 
                                             card_id:car_id, 
                                             price:price, 
                                             weight:weight, 
                                             name:name, 
                                             date:date..
                                           }]
                       }
    
cars_with_battires = {
                      cars_with_battry: [{id:id, 
                                          name:name, 
                                          color:color, 
                                          size:size, 
                                          date:date..}, 
                                          battries:[{id: 1, 
                                                     name: name1
                                                     }, 
                                                     {id: 2, 
                                                     name: name2
                                                     }
                                                   ]
                                          ]}
render json: { 
          available_cars: available_cars, 
          available_batteries: available_batteries, 
          cars_with_battry: cars_with_battry }
          serializer: CarIndexListSerializer }

这是我的序列化器文件,我需要填充这些函数

#car_index_list_serializer.rb
class CarIndexListSerializer < ActiveModel::Serializer
   attributes :available_cars,
              :available_batteries,
              :cars_with_battry

   def available_cars
       #todo?      
   end

   def available_batteries
       #todo?
   end

   def cars_with_battry
       #todo?
   end

我可以删除不需要的字段并以JSON格式返回结果,但我希望使用active_model_serializer以下面的格式返回结果。

{
    available_cars:[{id: 1, name:name1, size: abc}],
    available_batteries:[{id: 1, name: name1}],
    cars_with_battry:[{id: 1, name:name1, size: abc, 
                       battries:[{id: 1, name: name1}]
                      }]
}

如果任何哈希值为空,我不想在结果中显示它

t40tm48m

t40tm48m1#

result = {}
available_cars = Car.where xxx 
if available_cars.count.positive?
  result[:available_cars] = available_cars.map {|car| CarSerializer.new(car).as_json }
end

# do the same thing for available battery and car with battery

render json: result

只是个大概

相关问题