0

I have a simple relationship set up between two models, collections and works. A collection has many works, and a work belongs to a collection.

The collection serializer:

#collection_serializer.rb

class CollectionSerializer < ActiveModel::Serializer
  self.root = true

  attributes :id, :title

  has_many :works, embed: :ids, embed_in_root: true, key: :works
  has_one  :user, embed: :id, key: :user
end

The work serializer

#work_serializer.rb

class WorkSerializer < ActiveModel::Serializer
  attributes :id, :title, :min_year, :max_year, :longitude, :latitude, :circa, :place, :created_at, :updated_at

  has_one   :collection, embed: :id, embed_in_root: false
end

When I call the collection, I call the collection from my application (Ember), I get SystemStackError - stack level too deep. Here's the controller in Grape that I use to call the collection:

module API
  module V1
    class Collections < Grape::API
      include API::V1::Defaults

      resource :collections do
        desc "Return a collection"
        params do
          requires :id, type: String, desc: "ID of the collection"
        end
        get ":id", root: "collection" do
          Collection.where(id: permitted_params[:id]).first!
        end
      end
    end
  end
end

It seems like a simple enough thing to do, but I can figure out this error. Thanks!

amhasler
  • 67
  • 1
  • 6

1 Answers1

0

I recommend you to create a new serializer for Work which wouldnt include collection.

Then in your work serializer:

has_many :works, embed: :ids, embed_in_root: true, key: :works, serializer: YourNewWorkSerializerClass
apneadiving
  • 114,565
  • 26
  • 219
  • 213