Anum Siddiqui
Sep 16, 2018 · 3 min read

Let’s talk about serializers in Ruby. Most people build wonderful applications in Ruby without having to use serializers. It’s definitely one of those things you can do without, until you really need it! In this blog we will talk about how to create and manage serializers.

Introduction to ActiveModel::Serializer

So what exactly is ActiveModel::Serializer? Well ActiveModel::Serializer provides a way of creating custom JavaScript Object Notation, otherwise known as JSON. It manages to do this by representing each resource as a class that inherits from ActiveModel::Serializer. This is very helpful when you want to build JSON API’s through serializer objects as this allows you to fully customize the JSON output. This way we can select only the information we want, as well as get access to our relationships (OOP).

Installation

In order to install ActiveModel::Serializer, add the following gem to your

Gemfile:

gem 'active_model_serializers', '0.9.3'

Once the gem is added, you can install it using bundle:

bundle install

After the installation has been complete, in order to generate a serializer, you must decide what model to generate the serializer for.

rails g serializer product

For example the above will create a serializer for the model Product. When the serializer is generated it will show up as app/serializers/product_serializer.rb with the following content:

# app/serializers/product_serializer.rb

Lets say our Product model had all these attribrutes:

class Product < ApplicationRecord  
has_many :reviews
has_many :users, through: :reviews
validates :title, presence: true
validates :description, presence: true
validates :category, presence: true
validates :price, presence: true
validates :lightURL, presence: true
validates :medURL, presence: true
validates :darkURL, presence: true
validates :vidURL, presence: true
end

But if we didn’t need all this information being rendered from our ProductSerializer, what we can do is once our product serializer has been generated, we can chose what attributes we want our JSON API to contain.

class ProductSerializer < ActiveModel::Serializer  
attributes :id, :title, :description, :category, :price
end

So now when we load http://localhost:3000/api/v1/products, we should see our API composed of custom data that we wanted to display by allowing it in our serializer.

 http://localhost:3000/api/v1/products[
{
"id": 16,
"title": "Plum",
"description": "Size: 3 g / 0.1 US oz",
"category": "Lipstick",
"price": "$18.50"
},
{
"id": 17,
"title": "Red",
"description": "Size: 3 g / 0.1 US oz",
"category": "Lipstick",
"price": "$18.50"
},
{
"id": 18,
"title": "Pink",
"description": "Size: 3 g / 0.1 US oz",
"category": "Lipstick",
"price": "$18.50"
},
{
"id": 19,
"title": "Nude",
"description": "Size: 3 g / 0.1 US oz",
"category": "Lipstick",
"price": "$18.50",
"imgURL": "/image
},
{
"id": 20,
"title": "Coral",
"description": "Size: 3 g / 0.1 US oz",
"category": "Lipstick",
"price": "$18.50"
}
]

As you can see, in my ProductSerializer I was able to omit attributes I didn’t need to be displayed and instead only add the ones I want and formatted into JSON. In this way, AMS(ActiveModel::Serializer) is a powerful way to compose an API with explicit, easy-to-maintain serializers, as opposed to keeping track of what things you do and don’t want to render at the controller or view level.

Anum Siddiqui

Written by

New York native Full Stack Web Developer on the hunt for new opportunities and always hungry to learn!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade