Active_model_serializers: Inherited serializers not working

Created on 16 Oct 2015  路  4Comments  路  Source: rails-api/active_model_serializers

Hi,

I am using AMS-master, I trying to extend one serialiser and use that as in one my controller actions, but its giving bellow error.

NoMethodError (undefined method `read_attribute_for_serialization' for #<User::ActiveRecord_Associations_CollectionProxy:0x007f81e42a08b0>):

My Serialisers are

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name
end

module Search
   class UserSerializer < UserSerializer
      def json_key
         :search_user
      end
   end
end

# Controller code is
  render json: Users.all, serializer: Search::UserSerializer

AMS-master, rails - 4.2.3

--Pratheepv

Needs Submitter Response 0.10.x

Most helpful comment

@PratheepV This happens because you're trying to serialize a collection with a serializer designed for a single resource. Replace serializer: Search::UserSerializer with each_serializer: Search::UserSerializer and it should work (this will use the generic collection serializer, and serialize each resource according to your UserSerializer).

All 4 comments

would you mind including your Ruby version the commit ref of master?

What have you tried to debug this?

@bf4,

commit version is 240387bc452fc494476885f59e428a6a00798fd0

ruby version - ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin13]

--Pratheepv

@PratheepV This happens because you're trying to serialize a collection with a serializer designed for a single resource. Replace serializer: Search::UserSerializer with each_serializer: Search::UserSerializer and it should work (this will use the generic collection serializer, and serialize each resource according to your UserSerializer).

@beauby , Yeah I tried your suggestion its working fine. Thanks.

ruby render json: Users.all, each_serializer: Search::UserSerializer

Was this page helpful?
0 / 5 - 0 ratings