I had the same error and found changing serializer: to each_serializer: in the controller solved the issue. Controller class RecordsController < ApplicationController … def artist @records = Record.where(‘artist_id = ‘ + params[:artist_id]) render :json => @records, each_serializer: RecordSummarySerializer end … end record_summary_serializer.rb – can remove the include line class RecordSummarySerializer < ActiveModel::Serializer attributes :id, :artist_id, … Read more
active-model-serializers
Use active_model_serializer with a non-ActiveRecord object
The model object needs to include it thusly: # active_model_serializers 0.10+ class ModelName include ActiveModel::Serialization end # active_model_serializers < 0.10 class ModelName include ActiveModel::SerializerSupport end This implements the methods needed within the object, and also auto-discovers the serializer matching the object name, so it can be used transparently just like an ActiveRecord object. This works … Read more
jbuilder vs rails-api/active_model_serializers for JSON handling in Rails 4
It depends on your preference and needs. If you are working with Ember.js front-end, I’d lean towards active_model_serializers since Ember.js was basically crafted to work well with it (Yehuda Katz is one of the maintainers of active_model_serializers and is on the core team for Ember.js; he gave a talk on the topic a while back). … Read more
Test ActiveModel::Serializer classes with Rspec
Assumptions This answer assumes you have the rspec-rails, active_model_serializers and factory_girl_rails gems installed and configured. This answer also assumes you have defined a factory for the Sample resource. Serializer spec For the current version(0.10.0.rc3) of active_model_serializers at the time of writing, ActiveModel::Serializer classes do not receive to_json and are , instead, wrapped in an adapter … Read more
Rails: Serializing deeply nested associations with active_model_serializers
Per commit 1426: https://github.com/rails-api/active_model_serializers/pull/1426 – and related discussion, you can see that the default nesting for json and attributes serialization is one level. If you want deep nesting by default, you can set a configuration property in an active_model_serializer initializer: ActiveModelSerializers.config.default_includes=”**” For detailed reference from v0.10.6: https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/general/adapters.md#include-option
How do you initialize an ActiveModel::Serializer class with an ActiveRecord::Relation array?
I think this is what you’re looking for: ActiveModel::ArraySerializer.new(Funding.all, each_serializer: FundingSerializer).to_json See this Thoughtbot blog post for an example.
Conditional attributes in Active Model Serializers
In the latest version (0.10.x), you can also do it this way: class EntitySerializer < ActiveModel::Serializer attributes :id, :created_at, :updated_at attribute :conditional_attr, if: :condition? def condition? #condition code goes here end end For example: class UserSerializer < ActiveModel::Serializer attributes :id, :username, :name, :email, :created_at, :updated_at attribute :auth_token, if: :auth_token? def created_at object.created_at.to_i end def updated_at … Read more
How to pass parameters to ActiveModel serializer
In version ~> 0.10.0 you need to use @instance_options. Using @Jon Gold example from above: # controller def action render json: @model, option_name: value end # serializer class ModelSerializer::ActiveModel::Serializer def some_method puts @instance_options[:option_name] end end