Search code examples
ruby-on-railsactive-model-serializers

Active model serializer 0.9.4 stack too deep error


Hi the serailizer in my application looks like this

class ProgressSerializer < ActiveModel::Serializer
  attributes :id
  has_one :race
end

class RaceSerializer < ActiveModel::Serializer

  attributes :id
  has_many :progresses 
end

The has_one and has_many together gives me error stack level too deep. Things I tried.

config/initializers/active_model_intializer.rb

ActiveModel::Serializer.setup do |config|
  config.embed = :ids
  config.include = true
end

Second thing I tried

class RaceSerializer < ActiveModel::Serializer

  attributes :id
  has_many :progresses , :serializer => ProgressSerializer
end

class ProgressSerializer < ActiveModel::Serializer
  attributes :id
  has_one :race , :serializer => RaceSerializer
end

Models

class Progress < ActiveRecord::Base
    belongs_to  :race
end

class Race < ActiveRecord::Base
    has_many :progresses
end

Solution

  • Can you share your Model.rb files?

    If Progress is main then Races underneath, this one should work.

    class ProgressSerializer < ActiveModel::Serializer
      attributes :id, :races
      has_many :races
    end
    
    class RaceSerializer < ActiveModel::Serializer
      attributes :id
    end
    

    then try to delete has_many :progresses and add has_one :race to Race with attributes :id, :race