DEV Community

Cover image for A Simple Guide to Active Model Serializers in Ruby on Rails
Jamelse
Jamelse

Posted on

A Simple Guide to Active Model Serializers in Ruby on Rails

Serialization is an important consideration for developing robust and efficient Ruby on Rails APIs. The process of serializing involves transforming complicated data structures, Ex: Ruby objects, into a form that can be quickly communicated and reassembled on the client-side. Active Model Serializers is a strong Rails gem that streamlines serialization. We'll look at what Active Model Serializers are, how to set them up, and how to use their features to develop clean and consistent APIs in this article.

What is Active Model Serializers?

Before we get into how to use AMS, we must first understand what the gem does. AMS offers a straightforward and adaptable means of managing JSON serialization in Rails applications. It works by putting a layer of abstraction between your Rails models and the JSON representation to the API. AMS allows you to simply specify the structure and relationships of your API answers without clogging up your controllers with serialization logic.

Set up / Installation

Installation:

Include the gem 'active_model_serializers' in your gemfile:
Image description

Set Up:

Now we need to generate an ActiveModel::Serializer for our whatever out model is. In this example, I will generate it for my Expense model. Thankfully, the gem provides a generator through naming convention. In your terminal, run:

Image description
This should generate a new folder in /app called serializers. In that newly generated folder, will be a newly generated ruby file calledexpense_serializer.rb. In my example, my Expense model has :name, :amount, and :date attributes. The file will look something like this-
In /app/serializers/expense_serializer.rb :
Image description
If we wanted to customize our JSON response, we simply change which attributes we include. For example if I wanted my JSON response to NOT include the :date attribute, I would simply remove it from the list:

Image description

Now that all of that is set up. When we call /expenses or /expenses/:id we should get our preferred JSON response!

Custom Methods

AMS also allows us customize information by creating instance methods on our ExpenseSerializer class. We can then add this attribute to our list and get that method rendered in our JSON response! For example, if we wanted to create a method that shows us a readable amount of our expense name and cost we could do something like:

Image description
This should add a readable_expense attribute to the end of our JSON response with something like, "Phone Bill: $120".

Associations:
If we have multiple associated models, AMS can handle this by using the Active Record macros has_many and belongs_to. We can use these macros to include them in our JSON response! For example, lets say we have a User Class along with our Expense Class. In this instance, the User will have many expenses, and an expense will belong to a user. Firstly, we must make sure both of our models contain this association.
In /app/models/user.rb
Image description
In /app/models/expense.rb
Image description
To add this to our serializers, it's as simple as including these macros in our UserSerializer and ExpenseSerializer classes.
Image description
Image description
In this case, this is a one-to-many association. The same can be done the same way for a many-to-many association. However, rather than including has many :users, through: :expenses like in our model. We only define this in our model and simply call the has_many macro in our serializer class file.

Conclusion

Active Model Serializers is a fantastic gem that simplifies the serialization process in Ruby on Rails applications. By following this guide, you can get started with AMS and create simple, clean and effective APIs. Consider using customization options where necessary, and look into using alternative adapters if you wish to utilize a different format than JSON. If you made it this far, thank you for reading, I hope this guide could have helped simplify some of the concepts and uses of AMS. Last but not least, best of luck and happy coding :) .

Top comments (0)