diff --git a/content/blog/ruby-on-rails-polymorphic-association-benefits.md b/content/blog/ruby-on-rails-polymorphic-association-benefits.md new file mode 100644 index 000000000..9dce1aa46 --- /dev/null +++ b/content/blog/ruby-on-rails-polymorphic-association-benefits.md @@ -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 + +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 + +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. + +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. + +--- + +## 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`. + +```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. diff --git a/static/images/2023/11/polymorphic-association.png b/static/images/2023/11/polymorphic-association.png new file mode 100644 index 000000000..b07987df2 Binary files /dev/null and b/static/images/2023/11/polymorphic-association.png differ