diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 5de2ae24..b50cb951 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -341,7 +341,8 @@ module ActiveModel next if reflection.excluded?(self) next unless include_directive.key?(key) - y.yield reflection.build_association(self, instance_options, include_slice) + association = reflection.build_association(self, instance_options, include_slice) + y.yield association end end end @@ -390,14 +391,12 @@ module ActiveModel # @api private def associations_hash(adapter_options, options, adapter_instance) - relationships = {} include_directive = options.fetch(:include_directive) - associations(include_directive).each do |association| - adapter_opts = adapter_options.merge(include_directive: include_directive[association.key]) - relationships[association.key] ||= association.serializable_hash(adapter_opts, adapter_instance) + include_slice = options[:include_slice] + associations(include_directive, include_slice).each_with_object({}) do |association, relationships| + adapter_opts = adapter_options.merge(include_directive: include_directive[association.key], adapter_instance: adapter_instance) + relationships[association.key] = association.serializable_hash(adapter_opts, adapter_instance) end - - relationships end protected diff --git a/lib/active_model/serializer/reflection.rb b/lib/active_model/serializer/reflection.rb index c9c965b1..d4f96390 100644 --- a/lib/active_model/serializer/reflection.rb +++ b/lib/active_model/serializer/reflection.rb @@ -37,13 +37,13 @@ module ActiveModel # 1) as 'comments' and named 'comments'. # 2) as 'object.comments.last(1)' and named 'last_comments'. # - # PostSerializer._reflections #=> - # # [ - # # HasOneReflection.new(:author, serializer: AuthorSerializer), - # # HasManyReflection.new(:comments) - # # HasManyReflection.new(:comments, { key: :last_comments }, #) - # # HasManyReflection.new(:secret_meta_data, { if: :is_admin? }) - # # ] + # PostSerializer._reflections # => + # # { + # # author: HasOneReflection.new(:author, serializer: AuthorSerializer), + # # comments: HasManyReflection.new(:comments) + # # last_comments: HasManyReflection.new(:comments, { key: :last_comments }, #) + # # secret_meta_data: HasManyReflection.new(:secret_meta_data, { if: :is_admin? }) + # # } # # So you can inspect reflections in your Adapters. class Reflection < Field