Serialize an array of models using active_model_serializers

I am trying to send a serialized version of a model to a view as a parameter using gem active_model_serializers

 #app/serializers/admin_serializer.rb class AdminSerializer < 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 => 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 <%= debug (ActiveModel::Serializer::Adapter.adapter_class(:json_api).new(ActiveModel::Serializer.serializer_for(@admins.first).new(@admins.first),{}).to_json) %> <%= debug (@admins.all.map{|admin| AdminSerializer.new(admin).to_json}) %> 

Above debugs give the answer below:

 --- '{"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}' 

In the first debug, I serialize only one object, and in the second I try to do this for an array of objects. The first debug correctly returns the serialized version of the object (in json_api format), and the second debug is missing. I also tried ArraySerializer, without success: ActiveModel::Serializer::ArraySerializer.new(@admins, each_serializer: AdminSerializer).as_json how to achieve the desired serialization. Moreover, if this is achieved, can I use another simplified version of this? Because this debug statement is too verbose.

Tried all the solutions mentioned here - How do you initialize the ActiveModel :: Serializer class with an ActiveRecord :: Relation array?

The main problem I'm trying to solve is that in the Admin controller index method, the Admin object is passed as PORO to the index.html file. But I want a serialized version of json of this object so that I can pass it to my reaction components as prop

index method returns correct json when shooting http://dashboard.localhost.com {000/ admins.json enter image description here

UPDATE # 1 for the index method

 def index @search = Admin.search(params[:q]) @admins_array = @search.result(:distinct => true).to_a if params[:page] @admins = @search.result(:distinct => 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 => 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 
+7
json ruby-on-rails ruby-on-rails-4 active-model-serializers json-api
Mar 29 '16 at 7:47
source share
2 answers

I have a controller that I need to specify in the serializer, because of the desire for different attributes from the default serializer.

In the controller:

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

So, just to make things work, what happens if you specify the each_serializer option?

edits:

External controller:

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

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

Change # 2,

Something strange seems to be happening with @admins data.

Try converting to an array:

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

Edit # 3

To split the 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) 
+14
Mar 29 '16 at 11:50
source share

Follow the instructions: Serialization before rendering the controller

You can 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 - output (using messages)

 '{"data":[{"id":"1","type":"posts","attributes":{"title":"first post","body":null}},{"id":"2","type":"posts","attributes":{"title":"second post","body":null}}],"links":{}} 
+1
Mar 29 '16 at 10:56
source share



All Articles