How to use Active Model Serializer without an Active Record Model?
Asked Answered
R

1

7

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?

Radiology answered 7/4, 2016 at 19:23 Comment(0)
R
8

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.

Radiology answered 9/4, 2016 at 21:45 Comment(2)
Up to date docs: github.com/rails-api/active_model_serializers/tree/…Warmonger
I also followed the docs of AMS and tried to do the same but Rails fails to find the serializer: [active_model_serializers] Rendered ActiveModel::Serializer::Null with User (0.09ms). My UserSerializer looks the same as your MyModel; What am I missing ? Here is the gist to describe it better.Rove

© 2022 - 2024 — McMap. All rights reserved.