Search code examples
ruby-on-railsplpgsqlpgactive-model-serializers

How to use Active Model Serializer without an Active Record Model?


I want to take the results of a pl/pgsql function, that returns a set of records and user AMS to serialize the results. How can I do this?


Solution

  • AMS can serialize a Plain-Old Ruby Object. AMS provides ActiveModelSerializers::Model which can easily make a PORO a serializable object by doing this:

    class MyModel < ActiveModelSerializers::Model
      attributes :id, :name, :level
    end
    

    MyModelSerializer would be the default serializer.