Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benefits of using polymorphic association in ruby on rails #185

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions content/blog/ruby-on-rails-polymorphic-association-benefits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
+++
title = "Unveiling the Benefits of Polymorphic Association in Ruby on Rails"
slug = "ruby-on-rails-polymorphic-association-benefits"
date = 2023-11-13T02:00:29+05:30
image = "/images/2023/11/polymorphic-association.png"
draft = false
authors = ["Syed Mohd Mehndi", "Bhavesh Choudhary"]
description = "Unveiling the Benefits of Polymorphic Association in Ruby on Rails"
tags = ["Software Craftsmanship", "Ruby on Rails", "Polymorphic", "Code Reuse", "Decoupling"]
categories = ["Software Craftsmanship", "Ruby on Rails", "Polymorphic", "Code Reuse", "Decoupling"]
type = ""
+++

In the ever-evolving landscape of web development, optimizing database design and ensuring efficient data management are crucial aspects.
Ruby on Rails, a popular web application framework, provides a powerful feature known as polymorphic association, which offers a myriad of benefits for developers.

In this blog post, we'll explore the advantages of leveraging polymorphic association in Ruby on Rails.

## 1. Storage Saving
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Efficient Storage Use?


One of the key advantages of using polymorphic association is the efficient use of storage.
Unlike traditional methods where blank columns might be created for existing records, polymorphic association allows data to be added to a dedicated table, avoiding the creation of unnecessary columns.

This not only saves storage space but also ensures a cleaner and more organized database structure.

## 2. Architectural Separation

Polymorphic association enables architectural separation by allowing developers to track specific functionality in a different table without impacting the main table responsible for holding core data.

This separation proves invaluable when dealing with functionalities that sync with other systems.
Developers can create similar tables for different sync functionalities, maintaining a clear and modular architectural design.

## 3. Flexibility

Flexibility is a hallmark of polymorphic association.
With a dedicated table for a specific functionality, such as tracking errors, developers have the freedom to add more columns like `last_error`, `error_code`, and others without impacting the main table.

This flexibility ensures that the database schema can adapt to changing requirements without the need for extensive modifications.

## 4. Better Indexing

Maintaining optimal performance is crucial for any application, and polymorphic association aids in achieving better indexing.
Adding numerous indexes to a single table can lead to performance degradation.

By separating functionality into dedicated tables, developers can create indexes tailored to specific needs without impacting the performance of the main table.
This approach contributes to a more efficient and responsive application.

## 5. Easy Attachability
Copy link
Collaborator

@2KAbhishek 2KAbhishek Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should explain attachability in this context as it is not that commonly used


Enabling functionality for multiple models becomes a seamless process with polymorphic association.
In our example, where functionality is enabled for three models, additional models can adopt the same functionality without requiring any database changes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we present the example before this?


Simply including the relevant module in the specific model is all that's needed, providing a hassle-free approach to extending functionality across different parts of the application.

## 6. Avoiding Code Duplication

The ease of attaching functionality to different models not only simplifies the development process but also helps in avoiding code duplication.

As mentioned in point number 5, incorporating a module for a specific functionality ensures that the same codebase can be reused across various models, promoting cleaner and more maintainable code.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can link the point no. 5 heading hete


---

## Illustrating Polymorphic Association with AWS Synchronization

Let's integrate a real-world example of polymorphic association for tracking AWS synchronization for two distinct models: `Patient` and `Doctor`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can say, "Let's look at/explore a real world example"


```ruby
# app/models/aws_sync_status.rb
class AwsSyncStatus < ApplicationRecord
belongs_to :parent, polymorphic: true
end

# == Schema Information
#
# Table name: aws_sync_statuses
#
# id :uuid not null, primary key
# parent_type :string not null
# synced_at :datetime
# triggered_at :datetime not null
# created_at :datetime not null
# updated_at :datetime not null
# parent_id :uuid not null
#
# Indexes
#
# index_aws_sync_statuses_on_parent (parent_type,parent_id)
#

# app/models/concerns/aws/sync_track.rb
module Aws::SyncTrack
extend ActiveSupport::Concern

included do
has_one :aws_sync_status, as: :parent, dependent: :destroy
delegate :synced_at, to: :aws_sync_status, prefix: :aws, allow_nil: true
end

def synced_with_aws_at
return nil if aws_record_id.blank?
aws_synced_at || created_at
end

def create_or_update_aws_sync_status(aws_sync_attrs)
aws_sync_status.blank? ? create_aws_sync_status(aws_sync_attrs) : aws_sync_status.update(aws_sync_attrs)
end
end

# app/models/patient.rb
class Patient < ApplicationRecord
include Aws::SyncTrack
end

# app/models/doctor.rb
class Doctor < ApplicationRecord
include Aws::SyncTrack
end
```

In this example, we've implemented a polymorphic association using the AwsSyncStatus model.
The Aws::SyncTrack concern is included in both the Patient and Doctor models, providing them with AWS synchronization tracking capabilities.

This modularity allows for easy attachment of the AWS synchronization functionality to multiple models without the need for extensive database modifications.

By utilizing polymorphic associations in this manner, developers can seamlessly extend functionality across various models, promoting a clean and modular approach to application development.

## Conclusion

Polymorphic association in Ruby on Rails emerges as a valuable tool for database management and application architecture.
From storage savings and architectural separation to flexibility and better indexing, the benefits of adopting polymorphic association are diverse and impactful.

By incorporating this feature into your Rails projects, you can enhance both the efficiency and maintainability of your application's database design.
The real-world example of AWS synchronization illustrates how polymorphic association facilitates the easy integration of specific functionalities across different models, showcasing the power and flexibility it brings to web development.
Binary file added static/images/2023/11/polymorphic-association.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.