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

add handling each_serializer when expose a collection of object. #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hlxwell
Copy link

@hlxwell hlxwell commented Jun 13, 2014

For now in the controller if you run

expose User.page(params[:page]||1), serializer: UserSerializer

It will give you an error, if you add :each_serializer it doesn't work.

so add the option when :each_serializer was used, that means user want to regards the object as a collection.

@mbhnyc
Copy link
Contributor

mbhnyc commented Feb 26, 2015

On this issue - i'm seeing serializers not working as well — am i alone on this??

NoMethodError - undefined method `read_attribute_for_serialization' for #<PortIn::ActiveRecord_AssociationRelation:0x007ff819c7af10>:
  activerecord (4.1.9) lib/active_record/relation/delegation.rb:136:in `method_missing'
  activerecord (4.1.9) lib/active_record/relation/delegation.rb:99:in `method_missing'
  active_model_serializers (0.9.0) lib/active_model/serializer.rb:101:in `block (2 levels) in attributes'
  active_model_serializers (0.9.0) lib/active_model/serializer.rb:155:in `block in attributes'
  active_model_serializers (0.9.0) lib/active_model/serializer.rb:154:in `attributes'
  active_model_serializers (0.9.0) lib/active_model/serializer.rb:260:in `serializable_object'
  rocket_pants (1.10.0) lib/rocket_pants/controller/respondable.rb:10:in `serializable_hash'
  rocket_pants (1.10.0) lib/rocket_pants/controller/respondable.rb:71:in `normalise_object'
  rocket_pants (1.10.0) lib/rocket_pants/controller/respondable.rb:92:in `normalise_object'
  rocket_pants (1.10.0) lib/rocket_pants/controller/respondable.rb:132:in `respond_with_object_and_type'
  rocket_pants (1.10.0) lib/rocket_pants/controller/respondable.rb:143:in `collection'
  rocket_pants (1.10.0) lib/rocket_pants/controller/respondable.rb:157:in `exposes'

@mbhnyc
Copy link
Contributor

mbhnyc commented Feb 26, 2015

For now, fixed with:

expose ActiveModel::ArraySerializer.new(@people, each_serializer: PersonSerializer)

@sevgibson
Copy link

You are not alone, @mbhnyc -- and the workaround precludes showing the pagination in the return data:

expose @people.paginate(:page => params[:page])

...
"count": 2,
"pagination": {
    "previous": null,
    "next": null,
    "current": 1,
    "per_page": 30,
    "count": 2,
    "pages": 1
}
...

@Sutto
Copy link
Owner

Sutto commented May 19, 2015

Hey guys - This isn't forgotten, I need to look back into it again but last time I worked on it I hit similar issues.

@Sutto
Copy link
Owner

Sutto commented May 19, 2015

fwiw, the easier solution for the moment is:

expose @people, serializer: ActiveModel::ArraySerializer, each_serializer: PersonSerializer

Which should handle pagination etc, child serializers and more. I'm looking at fixing this properly in a build (1.12 likely, if not that 1.13 in the near future) - but hit a few blockers last time I looked into it hence why it's not done yet.

Edit: If that above solution doesn't work, let me know and I'll work on fixing it properly.

@Sutto
Copy link
Owner

Sutto commented May 20, 2015

Can you guys take a look at the change on master? I've tested in a few places, and it appears to work - will release in 1.12 shortly (and add credit @hlxwell) - Sorry it's taken so dang long for me to get to this.

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

Successfully merging this pull request may close these issues.

4 participants