class ActiveModel::Serializer
def merge_association(hash, key, value, 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, value, unique_values) if current_value = unique_values[key] current_value.merge! value hash[key] = current_value.to_a elsif value hash[key] = value unique_values[key] = OrderedSet.new(value) end end