Skip to content

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

Open
hlxwell wants to merge 3 commits into
Sutto:masterfrom
hlxwell:master
Open

add handling each_serializer when expose a collection of object.#106
hlxwell wants to merge 3 commits into
Sutto:masterfrom
hlxwell:master

Conversation

@hlxwell

@hlxwell hlxwell commented Jun 13, 2014

Copy link
Copy Markdown

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

mbhnyc commented Feb 26, 2015

Copy link
Copy Markdown
Contributor

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

mbhnyc commented Feb 26, 2015

Copy link
Copy Markdown
Contributor

For now, fixed with:

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

@sevgibson

Copy link
Copy Markdown

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

Sutto commented May 19, 2015

Copy link
Copy Markdown
Owner

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

Sutto commented May 19, 2015

Copy link
Copy Markdown
Owner

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

Sutto commented May 20, 2015

Copy link
Copy Markdown
Owner

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