3

At times I'd like to use no serializer for a model, and other times I do. I have tried to request nil serializer, but it seems that a serializer is used regardless.

class API::FinancialDashboardSerializer < ActiveModel::Serializer
    attributes :id, :name, :weeks_remaining

    has_many :check_ins, serializer: nil
end

In this instance I'd like to return the association without any serializer, but it still uses the CheckInSerializer anyway.

Is there a way around this?

Jeremy Thomas
  • 6,240
  • 9
  • 47
  • 92

1 Answers1

0

I think you could just do:

class API::FinancialDashboardSerializer < ActiveModel::Serializer
  attributes :check_ins, :id, :name, :weeks_remaining
end

Or if that doesn't work:

class API::FinancialDashboardSerializer < ActiveModel::Serializer
  attributes :check_ins, :id, :name, :weeks_remaining

  def check_ins
    object.check_ins.map(&:as_json)
  end
end
lobati
  • 9,284
  • 5
  • 40
  • 61
  • I was trying to change the key name of an array of strings with `ActiveModel::Serializer::CollectionSerializer.new` but was facing `No serializer found for resource` error. Your second option solved my problem. Thanks. – Ramon Dias Nov 28 '22 at 14:13