From 725c02bf221c9ba44efef1d5d63909e552b4a49e Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Tue, 7 Mar 2017 21:14:15 -0800 Subject: [PATCH] fix weird double-embed issue --- app/serializers/api/v2/application_serializer.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/serializers/api/v2/application_serializer.rb b/app/serializers/api/v2/application_serializer.rb index 81772577..a64ef021 100644 --- a/app/serializers/api/v2/application_serializer.rb +++ b/app/serializers/api/v2/application_serializer.rb @@ -36,13 +36,15 @@ module Api end has_many(attr, opts.merge(if: -> { embeds.include?(key) })) do list = Pundit.policy_scope(scope[:current_user], object.send(attr)) || [] - child_serializer = "Api::V2::#{attr.to_s.singularize.camelize}Serializer".constantize + child_serializer = opts[:serializer] || "Api::V2::#{attr.to_s.singularize.camelize}Serializer".constantize resource = ActiveModelSerializers::SerializableResource.new( list, each_serializer: child_serializer, scope: scope.merge(embeds: []) ) - resource.as_json + # resource.as_json will return e.g. { users: [ ... ] } for collaborators + # since we can't get the :users key, convert to an array and use .first.second to get the needed values + resource.as_json.to_a.first.second end else id_opts = opts.merge(key: "#{key}_id") @@ -56,7 +58,7 @@ module Api serializer: child_serializer, scope: scope.merge(embeds: []) ) - resource.as_json + resource.as_json.to_a.first.second end end end