rocket_pants
rocket_pants copied to clipboard
add handling each_serializer when expose a collection of object.
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.
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'
For now, fixed with:
expose ActiveModel::ArraySerializer.new(@people, each_serializer: PersonSerializer)
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
}
...
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.
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.
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.