DEV Community

Cover image for Sending Better Data With Rails Serializers
Raymundo Alva
Raymundo Alva

Posted on

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
Enter fullscreen mode Exit fullscreen mode

After that we want to set up our model associations.

Creating Models

ext we have to set up our controllers.

$ rails g controller pokemons  
$ rails g controller trainers
Enter fullscreen mode Exit fullscreen mode

This will be our controller for Pokemon.

Controller For Pokemon

And this will be our controller for trainers.

Controller For Trainers

Now we have to define our routes like so.

Defining Routes

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

Running Migrations

$ rails db:migrate  
$ rails db:seed
Enter fullscreen mode Exit fullscreen mode

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.

Without Serializer

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’
Enter fullscreen mode Exit fullscreen mode

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

$ rails g serializer Pokemon
Enter fullscreen mode Exit fullscreen mode

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.

Creating Serializer

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

GET Request Results

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.

Showing Trainer Data

Finally our data will look like this.

Resulting Data

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! 😎

Top comments (0)