class ActiveModel::Serializer

def embedded_in_root_associations

def embedded_in_root_associations
  associations = self.class._associations
  included_associations = filter(associations.keys)
  associations.each_with_object({}) do |(name, association), hash|
    if included_associations.include? name
      association_serializer = build_serializer(association)
      # we must do this always because even if the current association is not
      # embedded in root, it might have its own associations that are embedded in root
      hash.merge!(association_serializer.embedded_in_root_associations) do |key, oldval, newval|
        if oldval.respond_to?(:to_ary)
          [oldval, newval].flatten.uniq
        else
          oldval.merge(newval) { |_, oldval, newval| [oldval, newval].flatten.uniq }
        end
      end
      if association.embed_in_root?
        if association.embed_in_root_key?
          hash = hash[association.embed_in_root_key] ||= {}
        end
        serialized_data = association_serializer.serializable_object
        key = association.root_key
        if hash.has_key?(key)
          hash[key].concat(serialized_data).uniq!
        else
          hash[key] = serialized_data
        end
      end
    end
  end
end