class ActiveModel::Serializer
def merge_association(hash, key, serializables, unique_values)
avoids the need to scan through the Array looking for entries every time
a unique list of all of the objects that are already in the Array. This
In order to make this efficient, we store a :unique_values hash containing
of all tags for all comments of the post.
which has_many tags, the top-level :tags key will contain the merged list
content for all of the children. For instance, if a Post has_many comments,
In some cases, an Array of associations is built by merging the associated
def merge_association(hash, key, serializables, unique_values) already_serialized = (unique_values[key] ||= {}) serializable_hashes = (hash[key] ||= []) serializables.each do |serializable| unless already_serialized.include? serializable.object already_serialized[serializable.object] = true serializable_hashes << serializable.serializable_hash end end end