Active Record Associations
An association refers to a connection between two Active Record. Association aid in making common operations simpler as well as easier in the code. They are implemented through the use of macro-style called in order for one to add features in the model
2. Types of associations
The various types of associations include
belongs_to association: it establishes a relation with another model in a way that every instance of the declaring model “belongs to” a single instance of another model.
According to the class diagram one book is assigned to a single author.
has_one association : it shows that another model has a reference to the model. The model can be fetched using this association.
Every supplier has one account
has_many association : it represents a one to many relation with another model. The association shows that every instance of the model has none or more instances of another model.
Based on the association a book can have zero or many authors
has_many :through association : it is mostly used to establish a many to many relation with another model. The association shows that the declaring model can be connected with zero or more instances of other models through a third model.
The association represents medical practice where patients make appointments to see doctors. The association indicate that the declaring model can be aligned with zero or many instance of other model through a third model
has_one :through association : it is used to set up a one to one relation with other model. The association shows that the declaring model can be connected with a single instance of another model through proceeding through a third model
This association shows that the declaring model can be matched with a single instance of another model through proceeding to another model. Every supplier has a single account and every account is associated with a single account history
has_and_belongs_to_many association : it aids in creating a many to many relation with another model without an intervening model. The association shows that every instance of the declaring model is referring to zero or more instances of another model.
How to make efficient use of Active Record associations in your Rails applications
The various methods that can be used to make efficient use of Active Record Associations in the Rails applications include:
Controlling caching: the association are built around catching, which keeps the output of the recent query available for more operations.
Avoiding name collisions: one is not allowed to use any name for the association. This is because creating an association adds a method with the name to the model. It is not advisable to given an association a name that is already used in an instance method of ActiveRecord::Base.
Updating the schema: one is responsible for maintaining the database schema to match with the associations.
Controlling Association Scope: this is essential when declaring Active Record models within a module
References
RailsGuides. (n.d.). Active Record Associations. Retrieved from https://guides.rubyonrails.org/association_basics.html