Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embed_in_root_key breaks on multiple objects #705

Closed
jemgold opened this issue Oct 29, 2014 · 0 comments
Closed

embed_in_root_key breaks on multiple objects #705

jemgold opened this issue Oct 29, 2014 · 0 comments

Comments

@jemgold
Copy link

jemgold commented Oct 29, 2014

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!

@bf4 bf4 mentioned this issue Oct 8, 2015
@remear remear closed this as completed Mar 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants