DEV Community

Cover image for Active Model Serializers
Barrett
Barrett

Posted on

Active Model Serializers

Active Model Serializers and their importance in building an API

"Active Model Serializers" is a ruby gem that you can add to your back-end application in order to control what is being displayed in your API database. It is very popular amongst the back-end developer community and is an excellent gem to use because not only does it allow us to show what attributes we would like to be shown but also other models that have a direct relationship with the current model that we are serializing.

Here, we will be going over how to properly install the "Active Model Serializers" gem to our Ruby gem file and also how to properly construct the serializer in a way that will allow us to show exactly what we would like our API to display for certain CRUD actions we are synchronizing up with our front-end application.

Installing our Active Model Serializers Gem

In order to use our serializers, first we must install the gem into our gem file. we can do that by first grabbing our "Active Model Serializers" gem file from "https://rubygems.org/gems/active_model_serializers/versions/0.10.13". We will copy the gem from this website and paste it into our gem file in our application like so

Image description

then we will run the "bundle install" command in our terminal. Then we are free to use our serializers as we please

Generating a Serializer for one of our models

Let's say we have our models set up with our relationships. One of our models is named Animal. we can generate a serializer for said model with a simple Rails generator command. The following would be written like so...

Image description

Once we have created our serializer, it should appear with the attributes that were generated through our Rails migration process. It should look something like this...

Image description

Adding Relationships to show in our serializer

Not only can you show the attributes for that model, but you can also show any existing relationships that our specific model has. If our Animal model has a many to many relationship with the visitors of the park in which our animals live. We can show the animal objects along with any visitors that wish to come see that specific animal or "belongs to" that animal. We can do that by adding "has_many :visitors" under our attributes, just like in the image provided above.

Parsing the seeded data into JSON

Once we have our serializers set up the way we like, we can go check out what data is being displayed based off of what our serializer is serializing our displayed data. The Animal model should now look like this once we fetch our data through "Postman" (An application to help show what our API is displaying for certain CRUD actions)...

Image description

As you can see all of the attributes in our Animal Serializer are showing as well as any visitors associated with that specific animal. Now that you know how to create a serializer for your Rails application go and try it out for yourself!

Top comments (0)