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

CollectionSerializer does not work when serializer is namespaced #2030

Open
kapso opened this issue Jan 15, 2017 · 6 comments
Open

CollectionSerializer does not work when serializer is namespaced #2030

kapso opened this issue Jan 15, 2017 · 6 comments

Comments

@kapso
Copy link

kapso commented Jan 15, 2017

Collection serializer does not work when serializer is namespaced inside a module.

So in the following case, it always defaults to just UserSerializer and not Admin::UserSerializer

ActiveModel::Serializer::CollectionSerializer.new(@users, each_serializer: Admin::UserSerializer)
@kapso kapso changed the title CollectionSerializer does CollectionSerializer does not work when serializer is namespaced Jan 15, 2017
@kapso kapso closed this as completed Jan 15, 2017
@bf4
Copy link
Member

bf4 commented Jan 15, 2017

Could you note why you closed this?

@kapso
Copy link
Author

kapso commented Jan 15, 2017

I used serializer: instead of each_serializer: and that fixed the issue.

@bf4
Copy link
Member

bf4 commented Jan 15, 2017

thanks for sharing back! did you find it in the docs? Just interested in your debugging path.

@bf4
Copy link
Member

bf4 commented Jan 15, 2017

@kapso I also recommend that you do not use the collection serializer directly. That's not how AMS is designed to be used.

@kapso
Copy link
Author

kapso commented Jan 16, 2017

@bf4 I had dig into the code for this. I am trying to upgrade from 0.9.3 to 0.10.4 and have been running into few issues and docs are hard to come by for this upgrade :(

Lot of our serializers are namespaced, for e.g. UserSerializer (root namespace), Admin::UserSerializer (under /admin), Api::UserSerializer (under /api)

This used to work in 0.9.3 but errors out in 0.10.4

@users = User.active # or User.active.to_a
render json: @users, serializer: Admin::UserSerializer, root: true

So I am forced to use

render json: {
  users: ActiveModel::Serializer::CollectionSerializer.new(@users, serializer: Admin::UserSerializer)
}

@kapso kapso reopened this Jan 16, 2017
@bf4
Copy link
Member

bf4 commented Jan 16, 2017

Some related issues C: Upgrading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants