CodeX
Published in

CodeX

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

--

--

--

Everything connected with Tech & Code. Follow to join our 900K+ monthly readers

Recommended from Medium

Survival Lore — Era I

Sharing my challenges and lessons learned in building an open source product

Introduction to postman variables and Data driven testing…

EN-FR Machine Translation with Various RNN Models in Google CoLab (1)

Covid-19 Tracker Application in PythonCoding:

Google Analytics: How To Automate Reporting With Google Sheet

The 2 Programming Languages You Should Learn Now

Operating with AppSync from AWS Lambda

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!

More from Medium

Awesome Ways to Loop and Iterate Over Arrays in Ruby

Working with ruby C extensions on Mac

Hot Glue Scaffold Builder for Rails Turbo Version 0.4.7

Rails: Nesting Data with Custom Serializers using Active Model Serializers