The Basics of Serializer: A Rails guide
Tags: rails, ruby, beginners, tutorial
Published: 2022-02-08 02:22:10

 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"
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1na71e7nqj1dsb5gcvnw.png)

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

```
rails generate serializer <Name of table>
```

![initial Camper Serializer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w47dkocr5afu62pyej4u.png)

![Initial Get Request with only :id](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2pftswuiltf362uzcwew.png)
 
 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ifguv6stzb69ps4ytgru.png)
Here we have increased the data returned by including more info from our table.
![Attributes returned from get request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0w46llrsd6xuv1kga32.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq4mct2fjnxkwivbeiqs.png)
 

 ![Serializer with added relationship](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dei2j3s1rew3ts93jdds.png)

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
```
Add the needed attributes, and then head to the camper controller.

![Controller with explicit serializer added](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugyq3so9zv91t0fqdk8s.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aclek8vn7ublpb436wo7.png)
Then add your custom method to the model.
 ![Custom method in Camper Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkuhkz6kqemwls0awzhn.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhyxnbc5l3prgtdqsgog.png)

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!