0

I am trying to upgrade to the active_model_serializers 0.10.x gem.

But I am getting an error:

can't add a new key into hash during iteration

Here are the relevant portions of code:

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @shows, each_serializer: ShowSerializer, meta: @shows.total_count, meta_key: 'count' }
end    

Serializer:

class ShowSerializer < ActiveModel::Serializer

  def attributes(*args)
    data = super

    performances = object.performances.order("billing_index ASC")
    display_limit = 7
    data[:performances] = ActiveModel::Serializer::ArraySerializer.new(performances.limit(display_limit), each_serializer: PerformanceSerializer, scope: self.scope)

    data
  end
end

I do not use

has_many :performances 

because I want to limit the performances to first 7.

ajayjapan
  • 349
  • 4
  • 18

1 Answers1

1

You should not override the attributes method unless you know what you are doing.

Here, what you can do, is simply override the association as such:

class ShowSerializer < ActiveModel::Serializer
  has_many :performances

  def performances
    object.performances.order('billing_index ASC').limit(7)
  end
end
beauby
  • 550
  • 3
  • 11