Making Table Relationships Models for One-to-Many & Many-to-Many Relationships in Ruby Active Record

Nick Frangione
3 min readApr 22, 2021

Active Record is an ORM (Object Relational Mapper), which means it allows developers to send queries to a database, which is a statement that retrieves data from the database. The Active Record Ruby gem makes fetching and representing data easier by storing the data in tables and creating relationships between them.

The most-common types of relationships between tables(types of data) are One-to-Many relationships, and Many-to-Many Relationships. Before going deeper into what these relationships mean, first we need to identify some things that we will need in order to draw out a model of our database’s table relationships.

  1. Identify the tables needed to store data in/model names (Category of Data).
  2. Identifying the relationship type between our tables (One-to-Many, Many-to-Many).
  3. Identifying the needed associations between our models based on the identified relationship type (“has_many”, “belongs_to”, “has_many … through: : …”)
  4. Identify any necessary attributes that we want want to store data for in our tables.

ONE-TO-MANY:

A One-to-Many relationship between tables means one instance of (point of data on) dataSetA can be connected to many instances of dataSetB, but one instance of dataSetB is only connected to one instance of dataSetA.

A One-to-Many relationship indicates that, for example, an (Pet) Owner can own many Pets, but a Pet can only be owned by one Owner. This relationship type requires an association for each model: an owner’s association to their pets: owner “has_many :pets”, and a pet’s association to their owner: pet “belongs_to :owner”. These associations will act as the bridge between the data stored on our separate.

DRAWN OUT IN MODEL:

As you can see, in our visual representation of our models and relationships, an instance of Owner can be connected to many instances of Pet, but each instance of Pet should only be tied to one owner, which is why our pet table has to contain its owner’s unique identification (owner_id).

MANY-TO-MANY:

A Many-to-Many relationship between tables means one instance of dataSetA can be connected to many instances on dataSetC, and one instance of dataSetC can be connected to many instances of dataSetA. We accomplish this through a join table, or what I like to think of as the reason the two data sets are connected. Our join table in this example, dataSetB, can only be connected one instance of dataSetA and one instance of dataSetC, and is the reason that dataSetA & dataSetC have a relationship at all.

A Many-to-Many relationship requires one extra type of association in order to make sure they each table is connected to the other two tables. This association is dataSetA “has_many dataSetB, through : :dataSetC”.

A Many-to-Many relationship indicates that, for example, a Restaurant can have many reviews, and can be connected to many different customers through the reviews they write. A Customer can write many reviews, and be connected to many Restaurants through the Reviews they write. BUT, a Review can only be connected to ONE Restaurant & ONE Customer.

DRAWN OUT IN MODEL:

As you can see, in the visual representation of our models and relationships, each instance of Review can only be written by one customer (belongs_to customer), and can only be about one Restaurant(belongs_to :restaurant). This is why our Reviews table has to contain the Customer ID & the Restaurant ID that each review is associated to, along with the rating given.

TAKE-AWAY:

If you are ever struggling to figure out where to start while creating/working with your active record database, and its table relationships, start by drawing out a visual representation of the desired models and table relationships within the database. This strategy to planning, makes the creation of a database a matter of creating models and migrations with our associations written in our models, and the attributes given to our table migrations.

--

--