Serialize an array of models using active_model_serializers

I have a controller that I need to specify the serializer in, due to wanting different attributes from the default serializer.

In Controller:

  def index
    search = User.ransack(search_params)
    render json: search.result, each_serializer: MembershipRenewalSerializer::MemberSerializer
  end

So, just to get things working, what happens if you specify the each_serializer option?

Edits:

Outside Controller:

ActiveModel::SerializableResource.new(
  User.first(2), 
  each_serializer: MembershipRenewalSerializer::MemberSerializer
).to_json

Note, that without specifying each_serializer, SerializableResource would use the UserSerializer.

Edit #2,

It looks like there is something weird happening with the @admins data.

Try converting to an array:

ActiveModel::SerializableResource.new(@admins.to_a).to_json 

Edit #3

To paginate your array, try the following:

@search = Admin.search(params[:q])
@results = @search.result(:distinct => true).to_a
@admins = Kaminari.paginate_array(@results).page(params[:page]).per(10)

Follow the guide: Serializing before controller render

You could use ActiveModel::SerializableResource.new(@admins, adapter: :json_api).to_json

in index.html.erb

<%= debug (ActiveModel::SerializableResource.new(@posts, adapter: :json_api).to_json) %>

below is the output(using posts)

'{"data":[{"id":"1","type":"posts","attributes":{"title":"first post","body":null}},{"id":"2","type":"posts","attributes":{"title":"second post","body":null}}],"links":{}}