DEV Community

Jack Cole
Jack Cole

Posted on

Using Serializers With a Ruby on Rails API

Using rails as a back end API is convenient due to the ease that we can render JSON. However, our default data that rails provides is burdened with often useless information. But with the Serializer gem, we are able to format our JSON easily on the back end without needing to do any manipulation on the front end.

Install

Once you have your rails API created you need to go to your Gemfile and add the line gem 'active_model_serializers'. Once your Gemfile is saved run bundle install.

Creating our serializer files

After migrating and seeding your database, it's time to generate our serializers. Run rails g serializer <model name> in your terminal for each of your models. Every model will require a serializer because this gem causes rails to look for your models corresponding serializer whenever you pass an object to a render json: line of code in your controllers.

Once your serializers are generated you will see that they are populated with a line that looks like attributes :id. Edit this line to include any attributes that you want displayed by when this object is rendered. For example, if a you were displaying a student you could add a some data that is associated with them such as their name, age, and gpa.

Associations

To continue with our example, lets say that our database had schools that each had many students and a student belongs to a school. When a student is called we could display information about their school by including it in the student serializer attributes. However, this would display the entire school object including all of that ugly data that we don't want. To only display certain information we will need to create a custom function.

Let's say our attributes line now looks like attributes :id, :name, :age, :gpa, :school. We could then specify what information we want displayed from the school by writing a method like so

def school
    {school_id: self.object.school.id, 
     school_name: self.object.school.name}
end
Enter fullscreen mode Exit fullscreen mode

One thing that should jump out to you is the use of self.object. The self is referring to the serializer, and the object is an attribute the serializer has that references the JSON object it is currently going through.

Hopefully with this information you can better understand serializers! Good luck!

Top comments (8)

Collapse
 
cescquintero profile image
Francisco Quintero πŸ‡¨πŸ‡΄

Hi Jack, active_model_serializers is an awesome gem and will work great in many projects but it is currently outdated.

You should take a look at Blueprinter gem which is a good replacement for AMS. πŸ‘πŸ½

Collapse
 
swiknaba profile image
Lud

I love blueprinter! Works well, nice features, beautiful DSL & code, good support and development. Have been using it across multiple projects since one year now.

Collapse
 
cescquintero profile image
Francisco Quintero πŸ‡¨πŸ‡΄

Yep. I was really worried in the first project I used it but as the time past, it proved itself and now it's my default gem for serialization :)

Thread Thread
 
swiknaba profile image
Lud

Read the code. It is really well written and they have a lot of tests. Understanding at least some parts of the code helps you reduce this anxiety :-)

Collapse
 
rhymes profile image
rhymes • Edited

Didn't know about it!

We use both Jbuilder and fast_jsonapi here at DEV.

Collapse
 
cescquintero profile image
Francisco Quintero πŸ‡¨πŸ‡΄

I knew about fast_jsonapi but when I was about to use it, Blueprinter looked better and preferred it but yeah, fast_jsonapi looks now like a very good choice as well.

Sadly, AMS is going away but it was a great tool in its prime time.

Thread Thread
 
rhymes profile image
rhymes • Edited

I'm glad both Blueprinter and fast_jsonapi can use Oj, best gem ever :D

Collapse
 
123jackcole profile image
Jack Cole

Good to know thanks! I'll check it out!