Rails Association: The Very Basics

Yoel Yukalo
3 min readNov 19, 2020

--

A connection between two Active Record Models is known as an association. With many types of database tables available, you’ll need to know the types of associations in order to properly navigate your database and pull the data you need when you need it.

  1. One to One
  2. One to Many
  3. Many to Many

One to One

One-to-one relations is stating that a model has exactly one instance of another model.

Example: Each User has only one account.

class user < ApplicationRecord    class account < ApplicationRecord
has_one :account belongs_to :user
end end

The user model contains the has_one method invocation and the account model contains the belongs_to method invocation. These invocations were used in this manner so that the foreign key is accessible in the account model.

One to Many

A one-to-many association indicates that an instance of a specific model is allowed to have many instances of another model, while the other model is only allowed to have only one instance of the first model.

Example: Each Author has many books, but each Book has only one author.

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

In this example, the book model is the model that contains the reference of the author's model in the form of a foreign key.

Many to Many

Many-to-many associations can be done using:

  • has and belongs to many
  • has many through

Has and Belongs to Many

A has_and_belongs_to_many association is an example of a type of association for many-to-many models. To utilize it, you must call has_and_belongs_to_many from both models.

Example: Let’s say, a director can have many different movies and the same movie may contain many directors, your models would be like this:

class Director < ApplicationRecord  class Movie < ApplicationRecord
has_and_belongs_to_many :roles has_and_belongs_to_many :directors
end end

However, a join table, an external table that connects two models, is needed to be made in order for this association to work. In the terminal, run:

create_join_table :director, :movie

class CreateDirectorMovie < ActiveRecord::Migration
def change
create_table :director_movies, id: false do |t|
t.references :director, index: true, foreign_key: true
t.references :movie, index: true, foreign_key: true
end
end
end

Has Many Through

The second way to proclaim a many-to-many relationship is through the “has_many through, ” association type. In order to use this association type, you’ll need to create a separate model whose sole purpose is to handle the connection between the initial two models.

Example:

class Cult < ApplicationRecord
has_many :bloodoaths
has_many :followers, through: :bloodoath
end
class BloodOath< ApplicationRecord
belongs_to :follower
belongs_to :Cult
end
class Follower< ApplicationRecord
has_many :bloodoaths
has_many :cults, through: :bloodoath
end

With this association, you will be able to get access to data specific to the 2 models relationship.

--

--

No responses yet