This should work with version 0.9.0
, and version 0.10.0
should probably support this out of the box when it is finally ready, but at the time of this answer, it was suggested that you not use that version (master/edge)
class MyArraySerializer < ActiveModel::ArraySerializer
def initialize(object, options={})
options[:each_serializer] = get_serializer_for(object)
super(object, options)
end
private
def get_serializer_for(klass)
serializer_class_name = "#{klass.name}Serializer"
serializer_class = serializer_class_name.safe_constantize
if serializer_class
serializer_class
elsif klass.superclass
get_serializer_for(klass.superclass)
end
end
end
You can modify the get_serializer_for
method to better suit your needs. I used this for handling STI subclasses where my parent class had all of the attributes defined. You will however need individual serializers for each of your individual objects since the attributes will most likely vary.
Then in your controller:
render json: @my_array_of_mixed_classes, serializer: MyArraySerializer