Before and After Ruby Serializers

If you’re new to Ruby Serializers or still have a hard time grasping it, I hope what I share will help clear up some confusion. I had to go through a lot of hand holding with my first couple of times using Serializer. I was able to appreciate Serializers after I stepped away from project for a while and restarted it on my own without looking at any previous code. I had come across some issues at a particular point and I remembered that’s when I was told to use Serializers in the past. The difference from then and now is that I have more time to deep dive into understanding Serializers. So, if you’re looking for a quick solution to get you by for now (such as a project for a bootcamp), you can jump down to the section “After Serializers — no methods needed, serializers do the work to show what you need!” However, I encourage you to take the time later on to fortify your knowledge.

What is a Serializer?

A Serializer will help render JSON data that you want to show in an API. This may not seem like a big deal, but it’s very helpful when you have model associations and you want to render JSON data in single API that represents related data between the models.

Serializers also help to maintain cleaner code, depending on what you need.

What will I show?

I will show how Serializers are helpful to render JSON data by sharing screenshots of code and the JSON output before and after using Serializer . My example will consist of three models: Users, Movies, and Reviews and attributes as shown below.

Generated models with their attributes.

Side Note: I used rails g resource review for the Review model (because I wanted to try it), which allowed me to build some of my association between the models in one command through my terminal. However the attributes look a little different compared to using rails g model. Notice the difference of the Review migration file compared to the other migration files which were generated using rails g model. Stick with rails g model as you practice until you’re comfortable with it. Then move onto using rails g resource. Check out a previous blog of mine if you need more information about rails g model and rails g controller.

In this example, users will have many reviews and can know which movie they reviewed. Movies can have many reviews and can know which user gave the review. So the Review model is what joins the User model and Movie mode.

Before Serializers — using methods to show what you need.

Code in Users Controller

Currently, the code only represents the relationships a user has to reviews and movies inuser.rb, as well as the rendering of JSON for all users and a single user in users_controller.rb

JSON output for Users

Here you see the result of the JSON data output from thedef index method in users_controller.rb. Notice we can see all the information about a user. It’s not good that we can see the password and we might not need the created_at and updated_at attributes necessarily.

Notice, we also do not see movie reviews that a user has made. So let’s update the code a bit to show a user’s reviews and movies that have been reviewed.

Updated Code

Notice how line 3 now has an include method for the review and movie model.

Image A — JSON output for Users with Reviews and Movies associated to a user.

Now we can see that our first user “Penny Wise” has a review with all of its attributes. There is also the movie that “Penny Wise” made a review on. However, this looks like a mess and we can still see the user’s password!

I’ll show just one way around showing a solution to a review’s content with some more updated code.

Updated Code Again

Image B
Image C

Notice in line 4 in Image B, I took out reviews in include and added methods: [:reviews_list]. I just added a method to render JSON data that is desired. In this case, I want to render just the review content.

The method reviews_list is in user.rb, the User model, which is shown in Image C. The code is grabbing the content value of each review of an instance of a user.

Image D

Here is the JSON data output using the method reviews_list. Take a moment to compare how the reviews are presented in Image A and Image D. It’s a little cleaner to see the reviews. However, we can still see the user’s password!

There are some simple ways and tedious ways to not show a user’s password. However, instead of going through another example of how to get one aspect of JSON data the way it’s desired such as not rendering the password, I’ll show you how to get what you want all in one spot using Serializers!

After Serializers — no methods needed, serializers do the work to show what you need!

Code in Users Controller

We’re starting at the beginning because we don’t need any methods. The code in user.rb and users_controller.rb will stay untouched. The serializer will render the JSON wanted.

First step to using Serializers: add gem 'active_model_serializers'to your Gemfile. Run bundle install. Now use rails g serializer user to create a Serializer for the User model. You should now have a new folder called serializers containing the file user_serializer.rb. It should look like:

Notice how it shows the attribute of :id. Let’s see what our Users JSON output looks like now.

We only see JSON for the users’ ids. Here is how I see the flow working for this top happen (I do not know the technical flow, this is just my logic to keep me going).

The render json: users in def indexfrom the controller has been activated. render json: users then goes to user_serializer.rb. Now the serializer determines what is shown in the JSON output. Currently that is just the :id. So let’s change that to see more user information such as the name, username, bio , age, and no password.

Updated Code

Notice how only what was needed was added. Since we don’t want to render the password, we simply do not ask for it.

Now we can see JSON output for data we only want to see for the user. Let’s include the reviews of users.

Updated Code

Line 3 shows the added code of has_many :reviews. Look at how it renders the JSON output.

This allows the reviews a user made. We can see that the user “Penny Wise” has made one review. Let’s render only id and content attributes of the review along with the movie that is being reviewed. We can do this by creating a serializer for the Review model using rails g serializer review and write the attributes that are needed.

Updated Code

The new code was created using rails g serializer review. I added the attributes of :content and :movie.

Now, we can see Penny Wise’s review and the movie associated with that review!

We can see there is some information we might not need to render in the “movie” hash. I’ll let you play with Serializers to render only what you want when rendering JSON for the movie model.


Serializers allow us to render JSON with more flexibility, ease, and neater code. As I mentioned before, I had my hand held in the beginning when learning about Serializers. I used this video when I was stuck in the beginning and I barely got through my project. I went trough it again later with time to digest it better. The video definitely made more sense. I hope the same happens for you when you return to your projects.

Serializers don’t seem intuitive at first, but they will become a friend once you understand them. Be patient when you get frustrated. Serializers will click with with practice, time, resources, and friends.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store