Rails API- Active Model Serializers

Selam Degefu
3 min readJul 8, 2021

If you have heard about serializers before but can’t seem to wrap your head around them, my goal with this blog is to help you understand how to use them. But first, what is a serializer? Serializer is used to customize the JSON rendered by our controller model. By default, all the data that is included in the rendered JSON is not neat or necessary to display on the client-side i.e created_at and updated_at dates so we can use serializer to not include that. Let’s take a look at a code example.

Let's say you want to bring in lists of dogs from the local host and render them on the page.http://localhost:3000/dogs
{
"id": 1,
"type": "German Shepard",
"year": 4,
"color": "brown",
"image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/3071/3071213_so.jpg",
"title": "The friendly dog"
"trained": true,
"description": "a well trained german shepard who is consitently busy looking for a bone to play with or have for dinner"
"created_at": "2021-05-21T17:11:35.682Z",
"updated_at": "2021-05-21T17:11:35.682Z"
}
......

Here we can see the created_at and updated_at attributes are generated automatically by rails so it is not relevant to the end-user. To start using Serialzers, add gem 'active_model_serailizers' in your gem file and run bundle install. Instead of manually creating ActiveModel::Serializer for our Dog model, we can rely on the gem to generate that for us. rails g serailzer dog will create the boilerplate for us and include an id attribute. This is where you can specifiy what information from the Dog controller you want to render on the page. In our scenario our Dogs serializer file would look like this:

class DogSerializer < ActiveModel::Serializerattributes :id, :type, :year, :color, :image_url, :siblings, :trained, :title, :descriptionend

The code above will eliminate the attribute that is not mentioned here. You are probably wondering how we can have two different render models and not have an issue with hierachy. Serializers have convention-based approach which means even if we have a dog model and dog serializer, by default Rails knows to use the serializer by simply calling render json:dog in our controller.

You can also use serailizers to associate data and create custom methods. Let’s say for instance you want to include a snippet of summary about the dog when you render the data, you can use serializer to create a custom method for that. It would look something like this:

class DogSerializer < ActiveModel::Serializerattributes :id, :type, :year, :color, :image_url, :siblings, :trained, :summarydef summary 
'#{self.object.title} - #{self.object.description[0..25]};
end end

With this you will see summary being included as an attribute for the dog model. Now let’s take a look at how you can use serializers to associate data. Let’s say your app allows users to favorite a dog. You most certanily don’t want user B to see user A’s favorited dogs as it doesn’t pertain to them. In this scenario you would want to create some type of favorite_dog model to have the favoried dog associated to the user. You will include that a user has_many favorite_dog so if you were to refresh your localhost:3000/dogs, you would see user with the list of their favorite_dog included like this:

{
"id": 1,
"type": "German Shepard",
"year": 4,
"color": "brown",
"image_url": "https://pisces.bbystatic.com/image2/BestBuy_US/images/products/3071/3071213_so.jpg",
"title": "The friendly dog"
"trained": true,
"description": "a well trained german shepard who is consitently busy looking for a bone to play with or have for dinner"
{
"favorite_dog":[
{
"id": 1,
"user_id": 1
}
]}
}

To summarize what I just went over Serialziers are powerful tool you can utlilize when building your application using rails. The serializer is used implicitly by Rails based on naming conventions; so if we want to override this, custom serializers can be explicitly passed in the controller. Serializers are used mostly to associate data and render custom models. I hope this was helpful. With more practice you will see the many benefits of using this simple tool.

--

--