Rails Associations (in Marriage terms)

Saddhana Arta Daniswara
Simpul Technologies
6 min readDec 4, 2019

--

Photo by The HK Photo Company on Unsplash

There are various types of associations in Rails. Sometimes you call associations as relationships. I am going to explain data relationships with terms of marriage.

First thing first, you should know why does data need to have a good relationship. With a good relationship, it will help you to find data faster and easier. If you ever wonder how come a good relationship can help, here is an example. Imagine that you call your lover as honey. Well, this is not a bad thing to do when you are the only one who calls your lover as honey. But if you are in a place where there are many people that calls their lover as honey too, it becomes harder to call your lover with only honey. You need something to differentiate your lover and other people's lovers. It is the same with data. You need to specify which data you want to get. You can do this by using relationships.

One-to-One Relationship

Photo by Olivia Bauso on Unsplash

Back to the topic, the example I gave you before is the example of one-to-one relationships. In the marriage term, we name this kind of relationship as “Monogamy”, it means that you only have one lover, and your lover only has you as his or her partner. With this type of relationship, when you cheat from your partner, your current relationship will be destroyed and you will get a new relationship with your new partner. So what we want to create is something like this.

One-to-One Relationship

To implement this type of relationship on rails, the first thing you need to do is create the table and model for User and Lover. If you have no idea how to create a table and model, you can go to this page.

After the table and model has been created, you need to open “user.rb” and “lover.rb” model inside this directory:

app/models/

Inside each model, you must define the relationship. What we want is one user only has one lover and one lover only has one user. So we can use has_one for the user and belongs_to for the lover.

class User < ActiveRecord::Base
has_one :lover, :class_name => 'Lover', :foreign_key => 'user_id'
...
end
class Lover < ActiveRecord::Base
belongs_to :user, :class_name => 'User', :foreign_key => 'user_id'
...
end

It means one User has one Lover and Lover belongs to User. It is pretty straightforward. After you create the relationship, you can call lover easily only by type:

user.lover

Do remind that user is not the table, but an instance of User. So you need to assign the User to an instance first. You can instantiate the user with:

user = User.find(user_id)

One-to-Many Relationship

Well, if you insist that you want more than one lover, don’t worry. You still can do that with this relationship. One-to-Many Relationship equals to “Polygamy”. This relationship lets you have more than one lover, but your partner only allowed to have one lover. This is what it looks like on the diagram.

One-to-Many Relationship

To create this relationship, you need to create the database and model, the same as before. Then you can edit each model inside the app/models/ directory.

For this relationship, we want user to has many lovers, and a lover only belongs to one user. So we use has_many for User and the same as before, belongs_to for Lover. It implemented like this:

class User < ActiveRecord::Base
has_many :lovers, :class_name => 'Lover', :foreign_key => 'user_id'
...
end
class Lover < ActiveRecord::Base
belongs_to :user, :class_name => 'User', :foreign_key => 'user_id'
...
end

The code above means one User has many Lovers and Lover only belongs to a User. After you create the relationship, you can call all user’s lover by:

user.lovers

If you want to get a specific lover you can use:

//if you want to get the user's first lover
user.lovers.first
//if you want to get the last
user.lovers.last
//if you want to get by id
user.lovers.find(lover_id)
//example
user.lovers.find(1)

With one-to-many relationship you can’t access a lover by only using “user.lover” because User has many lovers, so you need to tell which lover you want to access.

Do remind that user is not the table, but an instance of User. So you need to assign the User to an instance first. You can see how to instantiate the user in the last section of one to one relationship.

The other example of this relationship is the relationship between book and pages. A book has more than one pages. But a page can only belongs to one book.

Many-to-Many Relationship

This relationship is harder to implement both in real life or on rails. But you will find a lot of relationships like this more than two relationships before. In real life, we call this kind of relationship as an open relationship. With this relationship you are allowed to have more than one lover, and vice versa. To implement this type, it is a little bit different than the other. You need to create a join table to store the relationship because each Lover can have more than one user, so we can’t store the user_id relationship on the Lover table. But we still need to store the user_id foreign key, that is why we create the join table named Relationship table. You can see the model below.

Many-to-Many Relationship

After you create the required table you can open their models and edit it so it looks like this.

class User < ApplicationRecord
has_many :relationships
has_many :lovers, through: :relationships
...
end

class Relationship < ApplicationRecord
belongs_to :user
belongs_to :lover
...
end

class Lover < ApplicationRecord
has_many :relationships
has_many :users, through: :relationships
...
end

The code above means that a User can has many relationships and a Lover can has many relationships too. That is how we implement many-to-many relationship. To access User’s Lover you can do:

//if you want to get the user's first lover
user.lovers.first
//if you want to get the last
user.lovers.last
//if you want to get by id
user.lovers.find(lover_id)
//example
user.lovers.find(1)

You are able to get the lovers because you already declare at User that a User has many relationships, because of that you access the lover through relationships. With this kind of relationship, you are able to access the Lover’s User too. This is how you do it:

//if you want to get the lover's first user
lover.users.first
//if you want to get the last
lover.users.last
//if you want to get by id
lover.users.find(user_id)
//example
lover.users.find(1)

I will give you another example of this kind of relationship. The other example of many-to-many relationship is the relationship between books and authors. One book can be created by more than one authors, and an author can create more than one books.

There are some basic types of data relationships. Nothing is better than the other, they just need to be used in the correct situation. I hope after you read this story you can choose which one is the best for your situation. At least when you don’t have a good relationship with your lover, that won’t happen with your data.

--

--