Are you Serialize Clark?

Troy Meeker
4 min readMar 23, 2022

Similar to most things thus far in my coding journey, at first when the topic of serializers was first introduced, I found it confusing and hard to find their purpose. But the more I learned about them and tried my best to experiment with the topic on my own, I learned more about it and found them to be extremely helpful.

Lets dive in

Since we really only want the applications controller to be the communication piece between the model and the view, having it handle the code for organizing our app’s in a specific way is not really its responsibility, instead we can offload that responsibility to an entirely different category, a serializer.

Put succinctly, utilizing serializers in an application is a great way to easily customize and include or exclude the attributes of the JSON data that the application uses.

HOW?

First, we need to add the active-model-serializer gem into our Gemfile.

Next, run ‘bundle install’ in your console (inside the app’s folder) to activate the gem.

Now we are ready to actually create the serializer folder!

Say our model name is Car.rb, By entering into your console again and running ‘rails g serializer car‘, you will now generate a serializer for that Car model.

To customize our JSON, we simply provide the list of attributes that we want to be included, and remove the ones we do not need:

Now by default, the serializers specific attributes will be included when we call render json: car anywhere in our Cars controller.

Custom Serializers

Using Serializers also enables us to customize the information returned using an instance method on the CarSerializer class. For example, if we wanted to create a card for each car including its make, model, and image, we could do that by including the :card attribute to the existing list of attributes and creating a new method called card, and formatting our data how we like.

Voila!

But now I also want to include some information about the cars owner, How can I do that?

What else can Serializers do?

Well, thanks for asking! Serializers can also have the same association macros available to them that we often use in our models the most common of these include has_many, or belongs_to.

Since a car belongs_to an owner, we can add belongs_to :owner in our CarSerializer, and the JSON returned by our Car Controller, will now include all the info about that cars owner! (This also means that there will be an equal and opposite has_many :cars macro in the Owner Serializer)

Keep in mind that this nesting of data can get complicated quickly and these should only be used if we really need them.

Since we may not want all this data rendered with each example of a car, we can create another custom Serializer by running rails g serializer owner_car, This creates a new serializer where we can again customize the json that we want to include.

Lastly, we need to specify to the original Owner Serializer that when referencing the many cars that an owner may have, that we want this narrowed down set of data.

Now, Rails will still implicitly use the Owner Serializer to reference data for the owner, but will now use the simplified OwnerCarSerializer when rendering data for each car.

Serializers can seem daunting at first, but really do help simplify the process when working with a lot of data, or wanting to customize specific aspects of the data to use in your next application.

--

--