Bacancy Bacancy
  • Customers

      Fortune 500 Clients

      Verizon
      Verizon
      Franklin Templeton
      Franklin Templeton
      The Container Store
      The Container Store
      KPMG
      KPMG
      Academy Sports & Outdoors
      Academy Sports & Outdoors
      Shell
      Shell
      Bangkok Bank Ltd
      Bangkok Bank Ltd
      The Children's Place
      The Children's Place
      Edward Jones
      Edward Jones
      United Parcel Service
      United Parcel Service
      AdviceWorks
      AdviceWorks
      NatWest Markets
      NatWest Markets
      Bridgestone
      Bridgestone

      Technologies

      React JS VueJS Angular JS React Native Flutter Full Stack
      Ruby on Rails Node JS Golang AI & ML Data Science Java
      Python Salesforce DevOps Block Chain Mobile App Embedded & Hardware
      Explore All
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
  • what we do

      Experience

      Digital Workplace Services Product Enhancement

      Insights

      AI & Automation Data Analytics

      Innovate

      Blockchain Software Engineering Services Internet of Things (IoT) Data Science Artificial Intelligence Machine Learning

      Accelerate

      Agile & DevOps Cloud - Bacancy Combalt Digital Process Automation Open Source Microsoft Dynamics Salesforce

      Assure

      Quality Assurance IT Staff Augmentation

      Embedded & Hardware

      Product Engineering Analog Design Services FPGA Engineering Service Wireless Design Embedded Software Design

      not list

      Hardware Engineering Power Engineering IoT Hardware
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
  • who we work with

      Industries

      Banking Financial services & Insurance Startups Oil & Gas

      not list

      Healthcare Life Science Real Estate & Construction Logistics
      bacancy

      Let’s grow together Partner with us

      get quote
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
  • about us

      Culture

      Agile Mindset Bacancy Values Bacancy Culture

      About Company

      About Us Leadership Team Awards & Recognition Infrastructure

      not list

      Customer Reviews Our Locations Partnership Media Coverage

      WE ARE

      GREAT PLACE TO WORK - CERTIFIEDTM

      Building and sustaining High-Trust, High-Performance CultureTM

      Bacancy Great Place to Work
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
  • technology

      Front End

      Angular Reactjs Vue.js UI/UX

      Platforms

      MS Dynamics Salesforce

      Back End

      Ruby on Rails Node.js Golang Laravel .NET Java Python

      Mobile

      React Native Flutter Android iOS

      Cloud

      AWS DevOps

      QA

      Automation Testing Software Testing

      Embedded & Hardware

      Embedded
      bacancy

      World-class expertise,Delivered

      get quote
      Explore All
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
  • PRODUCTS

      EV Products

      Battery Management System Motor Controller AC Charger Explore All

      not list

      CCS2 Controller DC Fast Charger AC Charge Controller
      bacancy

      Let’s grow together Partner with us

      get quote
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
hire us

Bacancy

Bacancy represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.

12+

Countries where we have happy customers

1050+

Agile enabled employees

06

World wide offices

10+

Years of Experience

05

Agile Coaches

14

Certified Scrum Masters

1000+

Clients projects

1458

Happy customers

  • Customers
      Technologies
      • React JS
      • Angular JS
      • Vue JS
      • Node JS
      • AI & ML
      • Explore All
      Fortune 500 Clients
      • Verizon
      • The Container Store
      • KPMG
      • Academy Sports & Outdoors
      • Bangkok Bank Ltd
      • The Children's Place
      • Edward Jones
      • Franklin Templeton
      • AdviceWorks
      • NatWest Markets
      • Bridgestone
      • United Parcel Service
  • What we do
      Experience
      • Digital Workplace Services
      • Product Enhancement
      Insights
      • AI & Automation
      • Data Analytics
      Innovate
      • Blockchain
      • Software Engineering Services
      • Internet of Things (IoT)
      Accelerate
      • Agile & DevOps
      • Cloud - Bacancy Combalt
      • Digital Process Automation
      • Open Source
      • Microsoft Dynamics
      • Salesforce
      • Service Experience Transformation
      Assure
      • Testing
      • Resource Augmentation
      Embedded & Hardware
      • Product Engineering
      • Analog Design Services
      • FPGA Engineering Service
      • Wireless Design
      • Embedded Software Design
      • Hardware Engineering
      • Power Engineering
      • IoT Hardware
      • AL/ML
  • who we work with
      Industries
      • Banking Financial services & Insurance
      • Startups
      • Oil & Gas
      • Healthcare Life Science
      • Real Estate & Construction
      • Logistics
  • About Us
      About Company
      • About Us
      • Leadership Team
      • Awards & Recognition
      • Infrastructure
      • Our Locations
      • Partnership
      Culture
      • Agile Mindset
      • Bacancy Values
      • Bacancy Culture
  • Technology
      Front End
      • Angular
      • Reactjs
      • Vue.js
      • UI/UX
      Platforms
      • MS Dynamics
      • Salesforce
      Back End
      • Ruby on Rails
      • Node.js
      • Golang
      • Laravel
      • .NET
      • Java
      • Python
      Mobile
      • React Native
      • Flutter
      • Android
      • iOS
      Cloud
      • AWS
      • DevOps
      QA
      • Automation Testing
      • Software Testing
      Embedded & Hardware
      • Embedded
      Explore All
  • Products
    • Battery Management System
    • Motor Controller
    • CCS2 Controller
    • AC Charger
    • DC Fast Charger
    • AC Charge Controller
    • Explore All
  • Careers
  • Work @ Bacancy
  • Blogs
  • Resources
  • Customer Reviews
  • Contact
  • Get Quote
Active Model Serializers

How to Implement Active Model Serializers with Your Rails API?

Archita Nayak
Archita Nayak Technical Writer
Bhavik Parmar
Bhavik Parmar Software Engineer
Last Updated on June 2, 2022
April 19, 2021 5 min read

In this tutorial, we will learn how to make serializers for Rails APIs and implement them efficiently so that they can be accommodated to versioning APIs too.

Table of Content

1. What is Active Model Serializers?

2. Importance and Usage of Active Model Serializers

3. Active Model Serializers Tutorial: How to Implement Active Model Serializers with Rails API?

4. Alternative of Active Model Serializers

5. Conclusion

What is Active Model Serializers?

Serializer gem allows us to format our JSON easily. It will enable us to select only the data we want and access our relationships with a single request. Active Model Serializers provides a way of creating custom JSON in an object-oriented manner.

Active Model Serializer consists of two components:

  • Serializers: It describes which attributes & relationships to be serialized.
  • Adapters: It describes the way of serializing those attributes & relationships, i.e., how to serialize.

The serializer creates the relationship in the backend and then translates it to the frontend.

Importance and Usage of Active Model Serializers

Active Model Serializers offers a way to create custom JSON by representing each resource as a class inherited from Active Model Serializers. With the help of a serializer, we need to make a single request to the backend. With proper association done through serialization, the backend will fetch all the required data needed by request rather than requesting multiple times to multiple models.

The primary usage of Active Model Serializers is to create custom JSON responses. By default, Rails provides a way to render JSON, but we need a serializer if we want to customize it.

You might like to know: How Much Does Ruby on Rails App Maintenance Costs?

Active Model Serializers Tutorial: How to Implement Active Model Serializers with Rails API?

Here is the step-by-step guide to implementing Active Model Serializers with Rails API whose source code is available in the Github repository.

In this demo, we will be installing two dependencies-

⦿ active_model_serializers gem

○ To achieve the intention of serialization

⦿ faker gem

○ To mock (fill) data

Let’s get started with building our demo project for Active Model Serializer.

1. Create new project

Create a new project by executing the below-mentioned command

rails new active-model-serializer --api

2. Adding Gem

Add below gems in your Gemfile

  • active_model_serializers
  • rack-cors
  • faker (development & test environment only)

Install these newly added gems using this command

$ bundle install

3. Data Modeling

Firstly, we will create two models and connect them to One-to-Many relationships.

We’ve built an Employee model having attributes (name, email, date_of_birth, mobile_no, designation, salary). Which belongs_to Manager model having the attribute (name).

$ rails g model Manager name
$ rails g model Employee name email dob:date mobile
designation salary:number manager:references

The Manager has_many employees. At the same time, Employee belongs_to only one manager.

4. Perform Migration

After the model changes, migration is necessary. Run the below command

$ rails db:migrate

5. Configuration of Routes

We will now add routes for APIs-only methods. The routes can extend with other versioning.

# config/routes.rb

Rails.application.routes.draw do
  concern :api_base do
    resources :employees   
    resources :managers
  end

  namespace :v1 do
    concerns :api_base
  end
end

6. Pre-defined Data Added

Declare data seed file to insert mock data for defined models. We can also enter data manually into the database, but it is easy with the seed file by running a single command.

$ rails db:seed

Here’s the seeds.rb

# db/seeds.rb

Employee.destroy_all
Manager.destroy_all

managers = (1..20).map do
  Manager.create!(
    name: "manager"
  )
end

employee = (1..50).map do
  Employee.create!(
    name: "employee",
    email: "[email protected]",
    dob: "17-01-1988",
    mobile: "8879544321",
    designation: "Senior Developer",
    salary: 35_000,
    manager: managers.sample
  )
end

7. Define Model Serializer

We will define a serializer for each model indicating what data to be broadcasted over the network by serializing the model. The attribute we declared to be serialized can be further defined to explain what data to be passed for.

You can see the below example for the model serializer.

# app/serializers/v1/employee_serializer.rb

module V1
  class EmployeeSerializer < ActiveModel::Serializer
    attributes :id, :name, :email, :designation, :manager

    def manager
      {
        id: object.manager.id,
        name: object.manager.name
      }
    end
  end
end

# app/serializers/v1/manager_serializer.rb

module V1
  class ManagerSerializer < ActiveModel::Serializer
    attributes :id, :name, :employees

    def employees
      object.employees.map do |employee|
        {
          id: employee.id,
          name: employee.name,
          email: employee.email,
          designation: employee.designation
        }
      end
    end
  end
end

8. Define Controller

It’s time to define the controller for respective models to serve serialized objects over API by explicitly defining serializers for individual models. Here, we will define which serializer will be used for passing data by JSON.

You can see the below example for controllers.

# app/controllers/v1/employees_controller.rb

module V1
  class EmployeesController < ApplicationController

    def index
      employees = Employee.all.includes(:manager)  
// used includes method to prevent N-Query problem
      
      render json: {
        data: ActiveModelSerializers::SerializableResource.new(employees, each_serializer: EmployeeSerializer),
        message: ['Employee list fetched successfully'],
        status: 200,
        type: 'Success'
      }
    end

    def show
      employee = Employee.find(params[:id])
      
      render json: {
        data: ActiveModelSerializers::SerializableResource.new(employee, serializer: EmployeeSerializer),
        message: ['Employee profile fetched successfully'],
        status: 200,
        type: 'Success'
      }
    end
  end
end

# app/controllers/v1/managers_controller.rb

module V1
  class ManagersController < ApplicationController
    def index
      managers = Manager.all.includes(:employees)
      
      render json: {
        data: ActiveModelSerializers::SerializableResource.new(managers, each_serializer: ManagerSerializer),
        message: ['Manager list fetched successfully'],
        status: 200,
        type: 'Success'
      }
    end

    def show
      manager = Manager.find(params[:id])
      
      render json: {
        data: ActiveModelSerializers::SerializableResource.new(manager, serializer: ManagerSerializer),
        message: ['Manager profile fetched successfully'],
        status: 200,
        type: 'Success'
      }
    end
  end
end

9. End-point Response

The output for the request to fetch details of employees and managers APIs is as shown below.

// GET http://localhost:3000/v1/employees/1

        {
	  "data":{
	    "id":1,
	    "name":"employee",
	    "email":"[email protected]",
	    "designation":"Senior Developer",
	    "manager":{
	      "id":6,
	      "name":"manager"
	    }
	  },
	  "message":[
	    "Employee profile fetched successfully"
	  ],
          "status":200,
	  "type":"Success"
	}

// GET http://localhost:3000/v1/managers/6

        {
	  "data":{
	    "id":6,
	    "name":"manager",
	    "employees":[
	      {
	        "id":1,
	        "name":"employee",
	        "email":"[email protected]",
	        "designation":"Senior Developer"
	      },
	      {
	        "id":33,
	        "name":"employee",
	        "email":"[email protected]",
	        "designation":"Senior Developer"
	      },
	      {
	        "id":39,
	        "name":"employee",
	        "email":"[email protected]",
	        "designation":"Senior Developer"
	      }
	    ]
	  },
	  "message":[
	    "Manager profile fetched successfully"
	  ],
	  "status":200,
	  "type":"Success"
	}

10. Versioning Controller & Serializer

We can extend the API and serializers by versioning them.
For creating another version of the controller, create it under the path app/controllers/v2/employees_controller.rb

And for creating the serializer, create it under the path app/serializers/v2/employee_serializer.rb.

Remember that the class you’re creating for versioned API should fall under module V2 (or whatever version you’re defining for).

Quick Summary

Here is the list of steps we performed to build serializer APIs:

  • Created new project using rails new active-model-serializer --api
  • Added required gems
  • Defined models
  • Migrated the schema
  • Configured routes for the API
  • Defined serializer for the respective model
  • Implemented business logic in the controller
  • Fetched data from API endpoints

Alternative of Active Model Serializers

  • JSONAPI-RB- It is considered a highly performant and modular JSON:API. There's a vibrant community around it that has produced this Ruby library consisting of four micro-libraries and two frameworks.
  • Fast JSON API- It is a lightning-fast JSON:API serializer for Ruby Objects. It is believed to be 25 times faster than Active Model Serializers.
  • Blueprinter- It is a high-speed, declarative, and API agnostic serializer that uses composable views for reducing duplication.

Conclusion

So, I hope you have a clear understanding of What is Active Model Serializers? Implementation, Usage, and Alternatives of Active Model Serializers in Rails 6. If you are looking for a helping hand in your Ruby on Rails project, then you’ve landed on the right page. Get in touch with us to hire ROR developer with the desired skillset at your ease and convenience.


Expand Your Digital Horizons With Us.

Start a new project or take an existing one to the next level. Get in touch to start small, scale-up, and go Agile.


Or
E-mail us : [email protected]

Your Success Is Guaranteed !

Related Articles

Deploy Ruby on Rails Application on Heroku
Paridhi Wadhwani
Isha Gupta

December 23, 2022

Ruby on Rails

How To Deploy Ruby on Rails Application on Heroku – Tech Tutorial

By : Paridhi Wadhwani & Isha Gupta

Introduction For any developer, the most satisfying thing is to make their development available to each individual after building it as a reference source. So,...

6 Minute Read
Read More
Ruby on Rails Mobile App Development
Paridhi Wadhwani

November 24, 2022

Ruby on Rails

Ruby on Rails Mobile App Development: Broadened Perspective

By : Paridhi Wadhwani

Quick Summary: Ruby on Rails wins the heart of entrepreneurs when it comes to web development, but find out why Ruby on Rails mobile app...

8 Minute Read
Read More
Ruby on rails vs JavaScript
Ritwik Verma

November 11, 2022

Ruby on Rails

Ruby on Rails vs JavaScript: What To Look For In 2023?

By : Ritwik Verma

Quick Summary: Ruby on Rails and JavaScript have often posed a challenge for product owners to choose one. In this blog post, we have comprehensively...

15 Minute Read
Read More

india India (HQ)

Corporate House
15-16, Times Corporate Park, Thaltej, Ahmedabad, Gujarat 380059

USA USA

601 Brickell Key Drive, Suite 700, Miami, Florida, 33131, USA

canada Canada

71 Dawes Road, Brampton, On L6X 5N9, Toronto

australia-flag Australia

351A Hampstead Rd, Northfield SA 5085

UAE UAE

1608 Clover Bay, Business Bay, Dubai, UAE. PO Box 62049

sweden Sweden

Junkergatan 4, 126 53 Hagersten


Great Place to Work

Get in Touch

call-yellow-icon

Contact Number

+1 347 441 4161

gmail-icon

Email Us

[email protected]


  • Bacancy Behance
  • Bacancy Uplabs
  • Bacancy Pinterest
  • Brochure
  • Quality Assurance
  • Resources
  • Tutorials
  • Customer Reviews
  • Privacy Policy
  • FAQs
  • Contact Us
  • Sitemap
  • Employee
bacancy google review 4.6
bacancy google review
bacancy glassdoor review 4.5
bacancy glassdoor review
bacancy clutch review 4.8
bacancy clutch review
bacancy goodfirms review 4.5
bacancy goodfirms review
X

Get Our Newsletter

Be The First To Get The Latest Updates And Tutorials.

Request A Free Consultation