app/controllers/concerns/json_renderer.rb
def add_includes(root, resources, includes)
      associations = {}

      includes.each do |association_name|
        resources.each do |resource, resource_as_json|
          associated = resource.public_send(association_name)
          if associated.is_a?(ActiveRecord::Base)
            resource_as_json["#{association_name}_id"] ||= associated.id
            (associations[association_name.pluralize] ||= []) << associated
          elsif associated.nil?
            resource_as_json["#{association_name}_id"] ||= nil
            associations[association_name.pluralize] ||= []
          else
            resource_as_json["#{association_name.singularize}_ids"] = associated.map(&:id)
            (associations[association_name] ||= []).concat associated
          end
        end
      end

      # add items to the existing collection, but avoid calling as_json multiple times for the same object
      associations.each do |namespace, objects|
        root[namespace] = objects.uniq.map(&:as_json)
      end
    end
# fill in <association>_ids/<association>_id and objects into the json response
fill in _ids/_id and objects into the json response
fill in _ids / _id and objects into the json response
def add_includes(root, resources, includes)
      associations = {}
      includes.each do |association_name|
        resources.each do |resource, resource_as_json|
          associated = resource.public_send(association_name)
          if associated.is_a?(ActiveRecord::Base)
            resource_as_json["#{association_name}_id"] ||= associated.id
            (associations[association_name.pluralize] ||= []) << associated
          elsif associated.nil?
            resource_as_json["#{association_name}_id"] ||= nil
            associations[association_name.pluralize] ||= []
          else
            resource_as_json["#{association_name.singularize}_ids"] = associated.map(&:id)
            (associations[association_name] ||= []).concat associated
          end
        end
      end
      associations.each do |namespace, objects|
        root[namespace] = objects.uniq.map(&:as_json)
      end
    end
def add_includes ( root , resources , includes ) associations = { } includes . each do | association_name | resources . each do | resource , resource_as_json | associated = resource . public_send ( association_name ) if associated . is_a? ( ActiveRecord :: Base ) resource_as_json [ "#{association_name}_id" ] ||= associated . id ( associations [ association_name . pluralize ] ||= [ ] ) << associated elsif associated . nil? resource_as_json [ "#{association_name}_id" ] ||= nil associations [ association_name . pluralize ] ||= [ ] else resource_as_json [ "#{association_name.singularize}_ids" ] = associated . map ( & :id ) ( associations [ association_name ] ||= [ ] ) . concat associated end end end associations . each do | namespace , objects | root [ namespace ] = objects . uniq . map ( & :as_json ) end end
fill in <association>_ids/<association>_id and objects into the json response
fill in <association > _ids / <association > _id and objects into the json response
# add items to the existing collection, but avoid calling as_json multiple times for the same object
root
resources
includes