Published in


Sending Better Data With Rails Serializers

Using Ruby on Rails as an API is awesome because of its render :json method. The only problem is that many times the JSON data may contain many unnecessary things. We want more options to customize the data and we can do that using serializers. We can use serializers to format our JSON by selecting only the data that we really need as well as get access to model relationships. Let’s get started.

In this example we can imagine that you want to query a list of Pokemon and their trainer from your database. Let’s create two models.

rails g model Trainer name:string age:integer
rails g model Pokemon name:string poke_type:string

After that we want to set up our model associations.

Next we have to set up our controllers.

rails g controller pokemons
rails g controller trainers

This will be our controller for pokemon.

And this will be our controller for trainers.

Now we have to define our routes like so.

Now we can run the migrations and seed our database with these commands.

rails db:migrate
rails db:seed

Now that everything has been set up we can make a GET request to query a list of all the pokemon in our database. Without a serializer this is what our data is going to look like.

You can see that we have some information that we really don’t need. There is no need to know when the pokemon were created and updated. It would be nice to hide this information. Something else is that we only get an integer for the trainer_id. Each pokemon has its trainer but we don’t really get any of the trainer’s information other than the id. Let’s get this data looking better. First add the serializers gem to your gemfile

gem ‘active_model_serializers’

after that bundle install and you will be able to generate serializers very easily. Go ahead and run this command.

rails g serializer Pokemon

This will create a folder called serializers inside of your app folder with a pokemon_serializer.rb file in it. This is where you can add the attributes that you want to be shown in your rendering. For a pokemon we want the name, trainer_id, and type to show up so the serializer will look like this.

After you do a GET request the data you see will look like this.

This looks great! We don’t have the created or updated attributes showing up anymore. Each pokemon has a trainer but we don’t have any information about the trainer. We want to be able to show more than just the id so in our serializer we will change it to look like this.

Finally our data will look like this.

That is all! Using serializers is really easy and a very good way to customize the data that you are rendering. Try using this on your API and let me know what you think. Happy coding! 😎



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
Ray Alva

Ray Alva

Software engineer with a passion for building applications. I love nature, gaming, cooking and of course coding!