Module: ActiveModel::Serializer::Associations

Extended by:
ActiveSupport::Concern
Included in:
ActiveModel::Serializer
Defined in:
lib/active_model/serializer/associations.rb

Overview

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.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary (collapse)

Instance Method Details

- (Enumerator<Association>) associations(include_directive = ActiveModelSerializers.default_include_directive)

Parameters:

  • include_directive (JSONAPI::IncludeDirective) (defaults to: ActiveModelSerializers.default_include_directive)

    (defaults to the default_include_directive config value when not provided)

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_model/serializer/associations.rb', line 85

def associations(include_directive = ActiveModelSerializers.default_include_directive)
  return unless object

  Enumerator.new do |y|
    self.class._reflections.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)
    end
  end
end