Skip to content

embed_in_root_key breaks on multiple objects #705

Closed
@jem-computer

Description

@jem-computer

Hey - might be doing something stupid, but I think I've found a bug in 0.9.0 when specifying a root key for associated child objects, when viewing an array of the parent objects.

grape 0.9.0
grape-active_model_serializers 1.2.1
active_model_serialziers 0.9.0

class UserSerializer < ActiveModel::Serializer
  attributes :id, :full_name, :location, :age

  # this works
  has_many :profile_photos, embed: :ids, include: true

  # this doesn't
  has_many :profile_photos, embed: :ids, include: true, embed_in_root_key: :linked
end

It works when serializing one object:

serializer = UserSerializer.new(FactoryGirl.build(:user))
serializer.to_json
# { "users": { "id":1, "full_name":"Yehuda Katz", "location":"Portland", "age":21, "profile_photo_ids":[6]}, "linked" : { "profile_photos": [ { "id":6, "url":"http://placekitten.com/48" } ] } } 

it breaks when serializing multiple objects:

serializer = ActiveModel::ArraySerializer.new(users, each_serializer: UserSerializer, root: :users)
serializer.to_json
#     NoMethodError:
#       undefined method `concat' for #<Hash:0x007fe033555fa8>

I'm using grape so not instantiating ActiveModel::ArraySerializer.new manually, but stumped by the Hash error I went back to IRB to figure out where the error was coming from. As far as I can tell this isolates it to something wrong with my Serializer configuration, but I'm not sure how to take it further and debug what's breaking.

It's not the end of the world, but I'd like to be able to embed sideloaded objects in a linked key if possible!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions