geekelo / dsa_practice

This repo is maintained just for the purpose of daily data structures and algorithm practice and proficiency
1 stars 0 forks source link

Describe types of associations in Active Record. #31

Open geekelo opened 1 month ago

geekelo commented 1 month ago

In Active Record, which is the ORM framework used in Ruby on Rails, associations define the relationships between different models. These associations enable developers to express relationships between tables in a database using object-oriented concepts. Active Record supports several types of associations, each representing a different type of relationship between models. Here are the main types of associations:

  1. belongs_to: This association indicates a one-to-one or many-to-one relationship between two models, where each instance of the associated model belongs to exactly one instance of the parent model. The foreign key is typically stored on the table of the associated model. For example:

    class Book < ApplicationRecord
     belongs_to :author
    end
    
    class Author < ApplicationRecord
     has_many :books
    end

    In this example, each book belongs to an author, and an author can have many books.

  2. has_one: This association indicates a one-to-one relationship between two models, where each instance of the parent model has one associated instance of the associated model. The foreign key is typically stored on the table of the parent model. For example:

    class User < ApplicationRecord
     has_one :profile
    end
    
    class Profile < ApplicationRecord
     belongs_to :user
    end

    In this example, each user has one profile, and a profile belongs to a user.

  3. has_many: This association indicates a one-to-many relationship between two models, where each instance of the parent model can have multiple associated instances of the associated model. The foreign key is typically stored on the table of the associated model. For example:

    class Author < ApplicationRecord
     has_many :books
    end
    
    class Book < ApplicationRecord
     belongs_to :author
    end

    In this example, an author can have many books, and each book belongs to one author.

  4. has_many :through: This association allows for a many-to-many relationship between two models through a join model. It's useful when you need to associate two models with each other through a third model. For example:

    class Doctor < ApplicationRecord
     has_many :appointments
     has_many :patients, through: :appointments
    end
    
    class Patient < ApplicationRecord
     has_many :appointments
     has_many :doctors, through: :appointments
    end
    
    class Appointment < ApplicationRecord
     belongs_to :doctor
     belongs_to :patient
    end

    In this example, a doctor can have many appointments, a patient can have many appointments, and each appointment belongs to a doctor and a patient.

  5. has_one :through: Similar to has_many :through, this association allows for a one-to-one relationship between two models through a join model. For example:

    class Supplier < ApplicationRecord
     has_one :account
     has_one :account_history, through: :account
    end
    
    class Account < ApplicationRecord
     belongs_to :supplier
     has_one :account_history
    end
    
    class AccountHistory < ApplicationRecord
     belongs_to :account
    end

    In this example, a supplier has one account, and each account has one account history.

  6. polymorphic: This association allows a model to belong to more than one other model, on a single association. It's useful when a model can belong to multiple other models with a single association. For example:

    class Image < ApplicationRecord
     belongs_to :imageable, polymorphic: true
    end
    
    class Product < ApplicationRecord
     has_many :images, as: :imageable
    end
    
    class User < ApplicationRecord
     has_many :images, as: :imageable
    end

    In this example, an image can belong to either a product or a user.

These associations simplify database interactions by allowing developers to express relationships between models using object-oriented concepts. They provide methods for accessing and manipulating associated records, and they handle the complexities of database queries and joins behind the scenes.