0

In my app, a Post has_many Comments and a Comment belongs_to Post (modeled via the has_one relationship in my serializers).

Since I get the Stack Level Too Deep error message whenever I attempt to model both of these relationships in my serializers, I am wondering if I should keep the has_many associations or the has_one association.

I also have an initializer that embeds :ids and sideloads the data.

Thanks for any help guys. I googled this I swear!

Kurt Mueller
  • 3,173
  • 2
  • 29
  • 50

1 Answers1

0

For those relationships, your serializers should look like this:

class PostSerializer < ActiveModel::Serializer
  embed :ids

  attributes :id # add other attributes here

  has_many :comments
end

class CommentSerializer < ActiveModel::Serializer
  embed :ids

  attributes :id # add other attributes here

  has_one :post
end

If you're getting a stack level too deep error, it's most likely from not embedding the ids. Without the ids being embedded it would attempt to embed the comments inside the post, and for every comment, again embed the post, which then embeds the comments again and so on in an infinite loop.

You should also make sure that you're using the DS.ActiveModelSerializer and DS.ActiveModelAdapter in your ember app.

HeroicEric
  • 876
  • 5
  • 13
  • The ids are embedded. The `AMS` gem now recommends that you have an initializer that sets `config.embed = :ids` and `config.embed_in_root = true`. The Ember docs aren't too helpful in this regard. I ended up going with keeping my `has_many` relationships and killing the `has_one` relationship. EmberData seems to be picking it up. – Kurt Mueller Jul 16 '14 at 12:35