When using embed: :ids ; embed_in_root: true, and serializing multiple objects,

only the associated objects of the last object in the collection will actually
show up in the serialized data.

For example, if you serialize a collection of two posts, each containing one or
more comments, only the comments of the last post show up. The reason is a
Hash#merge wich overwrites the array rather than appending to it.

This commit fixes this by merging the collection arrays, rather than the top-level
hashes.
This commit is contained in:
Arne Brasseur
2013-11-11 14:30:34 +01:00
parent 99677c0c58
commit 1db96ec7a9
3 changed files with 51 additions and 1 deletions

View File

@@ -44,7 +44,10 @@ module ActiveModel
def embedded_in_root_associations
@object.each_with_object({}) do |item, hash|
hash.merge!(serializer_for(item).embedded_in_root_associations)
serializer_for(item).embedded_in_root_associations.each_pair do |type, objects|
hash[type] = hash.fetch(type, []).concat(objects)
hash[type].uniq!
end
end
end
end