DEV Community

Cover image for The Basics of Serializer: A Rails guide

Posted on

The Basics of Serializer: A Rails guide

Active Model Serializer is a gem that we can include with our rails app to improve control of data structure. With API calls, it is important to control what information is seen by the user and what info is extraneous and doesn't need to be passed along.

Let's run through an example for better understanding.

First, make sure to install the gem in your Gemfile, and then run bundle install to make sure the gem is active.

gem "active_model_serializers", "~> 0.10.12"
Enter fullscreen mode Exit fullscreen mode

Next, set up a table with our future information. In this example we have a many-to-many relationship utilizing a join table.

Many to Many table

After generating each model, controller, and modifying routes as needed, go ahead and

rails generate serializer <Name of table>
Enter fullscreen mode Exit fullscreen mode

initial Camper Serializer

Initial Get Request with only :id

Be aware that serializer uses convention over configuration and also CamelCases its class name, even if it is made with snake casing.

At this point what the above image is saying is that whenever the camper controller is responding to a route endpoint it will use the format of the serializer. Since only the :id is listed any camper endpoint used will return just the :id associated with it.

Serializer with attributes added
Here we have increased the data returned by including more info from our table.
Attributes returned from get request

We can also tap into the relationships we have set between the tables.

Nesting Data with Relationships

A quick way to incorporate the relationships into the data you get back is to use the built in convention over configuration of Serializer. To do this, merely use the relationship to the other table in the serializer like this

Serializer with added relationship

Serializer with added relationship

This allows us to use the relationships established in our tables to return back the activity data of the selected Camper.

What if we want to include different data for each Camper route(show, index ..etc) ?

Explicit Specifying of Serializer

Let us say we want our index route to just show our Campers with no other associated data but we would like our Show route to include activities that a specific camper enjoys. We can enable that with another custom serializer.

rails generate serializer Camper_Activity
Enter fullscreen mode Exit fullscreen mode

Add the needed attributes, and then head to the camper controller.

Controller with explicit serializer added

This tells our camper controller to override its normal use of CamperSerializer and use CamperActivitySerializer instead. This means that when the Camper#show route is called on it will display the data how we established in CamperActivitySerializer

Finally, one last useful element we can use to really control data format in Active Model Serializer is custom methods.

Custom Methods

Custom methods allow you to add methods in to modify existing data to produce unique outputs to include back in the response. For example, let's say we wanted to include average difficulty of activities for a camper.

First, add the attribute which will be the name of the custom method as a symbol.Added attribute to serializer
Then add your custom method to the model.
Custom method in Camper Model

This enables you to now include in the response the difficulty average of the Camper's activities by utilizing the power of AMS.

Final get request with Difficulty Average included

Hopefully this blog has given you a better understanding of some of the uses of Active Model Serializer that you can use in your future projects!

Top comments (0)