Using Serializers In Your Ruby Rails App

Joseph Patterson
3 min readDec 7, 2020

--

During my time attending a coding bootcamp I collaborated on a small social web app with a friend. We decided to use React for the frontend and Ruby on Rails for the backend API. We spent two days excitedly sketching out user stories, components, and state flow. As the attention shifted to the database, we realized much of our enthusiasm resided in the frontend work. We were both comfortable enough with Ruby Rails as an API and even though we enjoyed it, there were aspects of it that filled us with dread. Rails is a fantastic choice as an API as it can render JSON easily but how it serves up the data can be less than ideal. My previous school projects incorporated a mess of API calls that quickly became confusing and inefficient as functionality grew. Luckily I had fantastic instructors who must have heard me whining to a classmate and told me to look up serializers. After a quick search I knew my worries were over.

Ruby Serializers

Ruby uses an Active Model Serializer to format JSON data without having to do anything on the frontend. This is incredibly useful when it comes to fetching from the database. Say you needed to look up a user and any posts associated with that user to load on login. Without serializers, this would take separate API calls to retrieve. This is what gave me heartburn. Luckily with serializers, the user and their posts can be linked in the backend with active model associations and have the data accessible with a single API call.

“ActiveModel::Serializer provides a way of creating custom JSON by representing each resource as a class that inherits from ActiveModel::Serializer.” -Hendra Uzia

Installation

Active Model Serializer is a ruby gem. Simply type gem ‘active_model_serializers’ in your gemfile and then bundle install. You will now have access to the serializer.

Setup

Every model in your database will need a serializer (for the most part). Setup is straightforward using Rails generators. For example, you can make a serializer for a user model by typing “rails g serializer user”. Now you have access to a serializer file.

class UserSerializer < ActiveModel::Serializer
attributes :id, :name
end

You’ll notice the attributes we’ve chosen to include when we make a call to our user object are id and name. Simple enough. What if we want to also include a user’s post information to this call? The serializer uses association macros to do this:

class UserSerializer < ActiveModel::Serializer
attributes :id, :name
has_many :posts
end

Now the user object will have another key of posts with a hash including all of that user’s post information. This can be repeated to include any other data from other models as long as the association macro is correct.

Serial-ously?!

The power of serializers was immediately evident when we implemented it in our app. The backend did all the work, keeping our frontend code dry and free from excessive API calls. I enjoyed using serializers so much that I refactored my previous Rails Projects to include them.

While this is a basic implementation I encourage you look at the official docs to see its full potential:

https://www.rubydoc.info/gems/active_model_serializers/0.8.2/ActiveModel/Serializer

--

--