rails-api/active_model_serializers — pull request #541 by vikhyat (2014-04-03 14:23:24+00:00)
Title: Deal with combining embed_in_root: true and embed: :objects

Currently if you have a situation where you have three serializers A, B and C where A has_many B embedded as objects and B has_many C embedded as IDs and you want C to be embedded at the root level of _A_ while rendering A to JSON, the only way to have C picked up by `embedded_in_root_associations` is to specify embed_in_root: true for both B and C.

However this has the side effect of rendering B in two places, at the root and inside serialized A. This unnecessarily increases the size of the JSON payload and also has a significant effect on performance. Since embed: :objects and embed_in_root: true aren't really compatible anyway I believe a better behavior would be to not embed B in the root (since it has embed: :objects) but to include any of B's child associations that do have embed_in_root: true set, so C would be included at the root level.

This is kind of an edge case since I don't believe there are going to be a lot of applications would be using both embed: :objects and embed: :ids like this, but Discourse's serializers does this kind of thing pretty frequently.