我有两个相关模型
   
    parent= Activity
   
   它与成本有许多关联。
  
  Class ActivitySerializer: < ActiveModel::Serializer
ActiveModel::Serializer.config.key_transform = :unaltered
attributes :id, 
          :name, 
          :description
 ...
has_many costs, each_serializer: CostSerializer
end
  
   CostSerializer如下所示:
  
  class CostSerializer < ActiveModel::Serializer
ActiveModel::Serializer.config.key_transform = :unaltered
attributes  :id, 
          :amount, 
          :description
end
  
   结果是,活动数据看起来不错,但成本数据告诉我:
  
  relationships":{"costs":{"data":[{"id":"20","type":"costs"}]}
  
   如果我这样做,它会起作用(将listCosts添加到ActivitySerializer中的属性):
  
  def listCosts
  object.costs.map do |cost|
    CostSerializer.new(cost, scope:scope, root: false, event: object)
  end
end 
  
   输出有点不同,没有关系,但有我想要的序列化程序属性。
我正在使用Rails 5.1.3和AMS 0.10。