paint-brush
Active Record Associations in Rails: Beginners Guideby@fabgra20
1,664 reads
1,664 reads

Active Record Associations in Rails: Beginners Guide

by FabricioJanuary 5th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In Ruby on Rails, an association is a connection between two Active Record models. An association can be a connection of type one-to-one with another model (class) An association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model. Understanding associations is vital to create a web application that behaves as you expect and avoid a good dose of frustration. If the current class contains the foreign key must use "belongs_to" instead of "belong_to_many"
featured image - Active Record Associations in Rails: Beginners Guide
Fabricio HackerNoon profile picture

As Ruby on Rails developers, we do not have to worry too much about the operations in the database due to the Object-Relational-Mapping or ORM. The ORM allows us to focus more on the logic of the business while it handles the operations with the database on our behalf.

Understanding associations is vital to create a web application that behaves as you expect and avoid a good dose of frustration. In Rails, an association is a connection between two Active Record models.

Let's imagine we have a client asking us to build a hospital management web app. Depending on the hospital needs we would need to create some Active Record Models.

Now let's take a couple of these models as an example: one for the doctors and one for the patients. We need to tell Rails that a doctor has many patients and one patient might have one or more doctors, we achieve these trough associations.

Rails supports six types of associations:

belongs_to: Specific for relationships of type one-to-one (1: 1) with another model (class). This method can be used only if this class contains the foreign key. If the other class contains the foreign key must use

has_one
instead.

class DoctorOffice < ApplicationRecord
  belongs_to :doctor
end

has_one: Specific for relationships of type one-to-one (1: 1) with another model. This method can be used only if the other class contains the foreign key. If the current class contains the foreign key must use

belongs_to
instead.

class Doctor < ApplicationRecord
  has_one :doctor_office
end

has_many: Specify an association of one to many (1: N). The referenced model must implement

belongs_to
.

class Doctor < ApplicationRecord
  has_many :patients
end

has_many :trough: Specify an association of many to many (N: M) with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model.


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

has_one :trough: Specify an association of one to one (1: 1) with another model. This association indicates that the declaring model can be matched with one instance of another model by proceeding through a third model.


class Patient < ApplicationRecord
  has_one :account
  has_one :account_history, through: :account
end
 
class Account < ApplicationRecord
  belongs_to :patient
  has_one :account_history
end
 
class AccountHistory < ApplicationRecord
  belongs_to :account
end

has_and_belong_to_many: Specify a direct association of many to many (N: M) with another model, with no intervening model.


class Doctor < ApplicationRecord
  has_and_belongs_to_many :patients
end
 
class Patient < ApplicationRecord
  has_and_belongs_to_many :doctors
end

If you are new to Rails associations or Rails, in general, I hope this quick introduction contributed to clarify some of your doubts about this topic.