module ActiveModel::Serializer::Associations

Defines an association in the object should be rendered.

The serializer object should implement the association name as a method which should return an array when invoked. If a method with the association name does not exist, the association name is dispatched to the serialized object.

Public Instance Methods

associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil) click to toggle source

@param [JSONAPI::IncludeDirective] include_directive (defaults to the

+default_include_directive+ config value when not provided)

@return [Enumerator<Association>]

# File lib/active_model/serializer/concerns/associations.rb, line 86
def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
  include_slice ||= include_directive
  return unless object

  Enumerator.new do |y|
    self.class._reflections.values.each do |reflection|
      next if reflection.excluded?(self)
      key = reflection.options.fetch(:key, reflection.name)
      next unless include_directive.key?(key)

      y.yield reflection.build_association(self, instance_options, include_slice)
    end
  end
end