title: Serialize an array of models using active_model_serializers
body: <p>I am trying to send the serialized version of a model to a view as a param, using the gem <a href="https://github.com/rails-api/active_model_serializers" rel="noreferrer">active_model_serializers</a></p>
  
  <pre><code>#app/serializers/admin_serializer.rb
  class AdminSerializer &lt; ActiveModel::Serializer
    attributes :id, :email, :access_locked?
  end
  
  
  #app/controllers/dashboard/admins_controller.rb
    def index
      @search = Admin.search(params[:q])
      @admins = @search.result(:distinct =&gt; true).page(params[:page]).per(10)
  
      @page_entries_info = view_context.page_entries_info @admins
      # render json: @admins
      respond_to do |format|
        format.html
        format.js
        format.json {render json: @admins}
      end
    end
  
  
  
  #app/views/dashboard/admins/index.html.erb
    &lt;%= debug (ActiveModel::Serializer::Adapter.adapter_class(:json_api).new(ActiveModel::Serializer.serializer_for(@admins.first).new(@admins.first),{}).to_json) %&gt;
    &lt;%= debug (@admins.all.map{|admin| AdminSerializer.new(admin).to_json}) %&gt;
  </code></pre>
  
  <p>Above debugs are yielding the below response:</p>
  
  <pre><code>--- '{"data":{"id":"1","type":"admins","attributes":{"email":"tech@bluesapling.com","access_locked?":false}}}' //returned by the first debug
  
  
  ---
  - '{"object":{"id":36,"email":"aubrey_schmitt@feeneykoch.io","created_at":"2016-03-28T05:15:17.546Z","updated_at":"2016-03-28T05:15:17.546Z"},"instance_options":{},"root":null,"scope":null}'
  - '{"object":{"id":20,"email":"alysa_johnston@thompson.io","created_at":"2016-03-28T05:15:16.304Z","updated_at":"2016-03-28T05:15:16.304Z"},"instance_options":{},"root":null,"scope":null}'
  - '{"object":{"id":22,"email":"kristofer.langosh@kunzeluettgen.com","created_at":"2016-03-28T05:15:16.459Z","updated_at":"2016-03-28T05:15:16.459Z"},"instance_options":{},"root":null,"scope":null}'
  - '{"object":{"id":37,"email":"beryl_keler@wiza.biz","created_at":"2016-03-28T05:15:17.624Z","updated_at":"2016-03-28T05:15:17.624Z"},"instance_options":{},"root":null,"scope":null}'
  - '{"object":{"id":5,"email":"wilhelmine_buckridge@crona.io","created_at":"2016-03-28T05:15:15.139Z","updated_at":"2016-03-28T05:15:15.139Z"},"instance_options":{},"root":null,"scope":null}'
  - '{"object":{"id":14,"email":"edward_wisoky@corkery.net","created_at":"2016-03-28T05:15:15.838Z","updated_at":"2016-03-28T05:15:15.838Z"},"instance_options":{},"root":null,"scope":null}'
  - '{"object":{"id":27,"email":"leonor@jerde.biz","created_at":"2016-03-28T05:15:16.848Z","updated_at":"2016-03-28T05:15:16.848Z"},"instance_options":{},"root":null,"scope":null}'
  - '{"object":{"id":2,"email":"carley@wyman.net","created_at":"2016-03-28T05:15:14.873Z","updated_at":"2016-03-28T05:15:14.873Z"},"instance_options":{},"root":null,"scope":null}'
  - '{"object":{"id":10,"email":"ervin.gleichner@cremin.org","created_at":"2016-03-28T05:15:15.527Z","updated_at":"2016-03-28T05:15:15.527Z"},"instance_options":{},"root":null,"scope":null}'
  - '{"object":{"id":15,"email":"lonzo.dickens@johnscole.name","created_at":"2016-03-28T05:15:15.916Z","updated_at":"2016-03-28T05:15:15.916Z"},"instance_options":{},"root":null,"scope":null}'
  </code></pre>
  
  <p>In the first debug I am serializing only one object, while in the second one I am trying to do it for an array of objects.
  The first debug is correctly returning the serialized version of the object(in json_api format) while second debug is not.
  Tried ArraySerializer as well, with no success: <code>ActiveModel::Serializer::ArraySerializer.new(@admins, each_serializer: AdminSerializer).as_json</code>
  how do I achieve the desired serialization. Moreover, if achieved, can I used some other simplified version of this? As this debug statement is way too verbose.</p>
  
  <p>Tried all the solutions mentioned here - <a href="https://stackoverflow.com/questions/17542793/how-do-you-initialize-an-activemodelserializer-class-with-an-activerecordrel/17572992#17572992">How do you initialize an ActiveModel::Serializer class with an ActiveRecord::Relation array?</a></p>
  
  <p><strong>The basic problem which I am trying to solve is, in the index method of the Admin controller, the Admin object is passed as a PORO to the index.html file. But I want the serialized json version of this object so that I can pass it to my react components as a prop</strong></p>
  
  <p>index method is rendering proper json on firing <a href="http://dashboard.localhost.com:3000/admins.json" rel="noreferrer">http://dashboard.localhost.com:3000/admins.json</a> 
  <a href="https://i.stack.imgur.com/DZPuR.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DZPuR.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE#1</strong> for the index method</p>
  
  <pre><code>def index
      @search = Admin.search(params[:q])
      @admins_array = @search.result(:distinct =&gt; true).to_a
      if params[:page]
        @admins = @search.result(:distinct =&gt; true).page(params[:page][:number]).per(10)
        @admins_json_array = Kaminari.paginate_array(@admins_array).page(params[:page][:number]).per(10)
      else
        @admins = @search.result(:distinct =&gt; true).page(1).per(10)
        @admins_json_array = Kaminari.paginate_array(@admins_array).page(1).per(10)
      end
      @admins_json = ActiveModel::SerializableResource.new(@admins_json_array.to_a)
      ...
      ...
      ...
  end
  </code></pre>