Simplify Your Rails API Development with AMS
Janice Alecha
Posted on May 3, 2023
INTRODUCTION
In this post, I will share my experience using AMS in my fourth project for Flatiron School Software Engineering program. "Crime-Sleuths" is web blog application specially designed for individuals who have a keen interest in comprehending the truth and deciphering criminal behavior. It is also relevant for online crime detectives like me. I'll explain how I implemented Active Model Serializer in this project and demonstrate how it simplified the data serialization process, allowing me to focus on developing other features of the application. So, keep reading to discover the benefits of using AMS in your own projects.
Define ActiveModel: :Serializer
Let's start by discussing Active Model Serializers or AMS, a powerful gem for Ruby on Rails that allows you to customize and simplify the formatting of our JSON output by allowing us to easily select the data we need and access relationships with just single request. With Active Model Serializers, you can create custom JSON output in an object-oriented way, giving you more control over the formatting and structure of your data.
To use Active Model Serializers, you first need to install the gem and create a serializer for your model. This serializer is a Ruby class that inherits from ActiveModel::Serializer and specifies which attributes should be included in the JSON output.
In your #Gemfile make sure to add:
gem 'active_model_serializers'
then Run bundle install
Next, you'll need to generate an ActiveModel::Serializer for your models, which can be easily done using the provided generator within the gem. To do so, simply open your console and run the following command:
rails g serializer <model name>
After running the command, a serializer file for the specified model will be created in the app/serializers directory. Active Model Serializers (AMS) offers a convention-based solution for serializing resources. MEANING that if we have a Post model, we can also have a corresponding PostSerializer serializer, and Rails will automatically use this serializer if we use the command render json: post in our controller.
Initially, in my #app/controllers/post_controller.rb:
This appears to be straightforward but what if we wished to display nested data involving users who own this posts. In such a case, we may need to incorporate a nested resource. For instance, in my blogging application as an example where users are the owners of posts. In this scenario, we may want to exhibit something like this:
#app/controllers/post_controller.rb:
Creating JSON strings can be a tedious and error-prone process. Additionally, this method does not adhere to the principle of separation of concerns, which is crucial in typical Rails applications. In such apps, the controller retrieves data from the model and passes it to the view layer, which is responsible for presenting the data to users. Similarly, in this case, it is not the controller's responsibility to determine how the data is returned. This is where ActiveModel::Serializer comes in handy.
Serializing Associated Data
My blog app represents many-to-many relationship between three models: Post, User, and Channel.
In addition to the attributes, two associations are defined using the belongs_to macro: :user and :channel. These associations specify that the Post model has a foreign key relationship with both the User and Channel models.
When the PostSerializer is used to serialize a Post object, the serializer will automatically include the associated User and Channel objects in the serialized output. This means that the serialized JSON response will include the User and Channel objects in addition to the Post attributes.
This allows for easy retrieval of associated data without needing to make additional API calls or construct complex JSON objects manually.
#app/controllers/post_serializer.rb:
Now in my #app/controllers/post_controller.rb I can revert to its initial state:
How to Implement Custom Serializers
When working with deeply nested models in Rails, it may be necessary to explicitly customize the serialization of the data to ensure that only the necessary information is returned. In the example I provided, I customize serializer for the UserPostSerializer to render users and their posts in my Channel route. Additionally, I defined a short_content instance method to limit the content of the posts returned to the first 100 characters.
To include the custom serialization, the UserPostSerializer is specified in the has_many association for the posts in the ChannelSerializer. This guarantees that the serialized channel data includes the custom serialization defined in the UserPostSerializer for the post data. By explicitly setting the serializer for the association, developers can ensure that only the required data is returned in the desired format.
Now when you run the server rails s and open browser to http://localhost:3000/channels, it should return:
CONCLUSION
I've found ActiveModel::Serializer to be a valuable tool for customizing the JSON data returned in my blog app project. It gave me the control to create serializers for specific resources and include only the data I need. AMS also follows Rails naming conventions made it intuitive to use.
Using the belongs_to and has_many macros in serializers, I was able to render associated data with ease. However, it's important to use these macros sparingly to avoid overwhelming the JSON data with excessive nesting.
It's worth noting that by default, AMS only nests associations one level deep in the serialized JSON. To go deeper, I used the include option to specify which associations to include.
Overall, ActiveModel::Serializer has helped me to customize the JSON data in my Rails application, ensuring that it's presented in the desired format.
RESOURCES:
Posted on May 3, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.