Sending Better Data With Rails Serializers
Tags: ruby, rails, beginners, tutorial
Published: 2023-05-03 05:00:01

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.
```bash
$ 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.

![Creating Models](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gww65rjgpggplpzz35yb.png)

ext we have to set up our controllers.
```
$ rails g controller pokemons  
$ rails g controller trainers
```
This will be our controller for Pokemon.

![Controller For Pokemon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rjtctt3zctdsnjzju2uw.png)

And this will be our controller for trainers.

![Controller For Trainers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fu419t3g6errmaepx3b9.png)

Now we have to define our routes like so.

![Defining Routes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ng39h2q1l2nvqux8mo4l.png)

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


![Running Migrations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjvm36h8bg9lihb36tus.png)
```
$ 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.


![Without Serializer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxxyikn29yp56i35m4at.png)

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.

![Creating Serializer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cj640idni5jyvrxftsud.png)

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

![GET Request Results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ltdrjgfqwvwwebxavdj3.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kvnaw00ittke7myur28.png)

Finally our data will look like this.


![Resulting Data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oic9k23yn09spe8x48gd.png)

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