DEV Community

Cover image for Associations


lycho33 profile image lycho33 ・2 min read

After learning Ruby on Rails, I learnt that associations was the most crucial aspect, without it...nothing works.

Original Project

The first project I created was supposed to be website where the user could be a student or mentor. If the user was a mentor, the mentor could create lessons but if the user was as student, the student would complete lessons. Simple?

Alt Text

For an idea I thought was so simple, turned out to be far too complicated for me to handle.

Problem 1

It was unnecessary to have 2 separate models for Students and Mentors.

So I decided to combine them into Users. But this did not work because I was bypassing Separation of Concerns. A User model was supposed to only deal with login/signout/logout. Instead I created a separated model called Role so the User could choose it's role as either a mentor or student

Next Problem

I wanted to create a many-to-many association but I had to combine my Student/Mentor model. By doing so I would have to create custom associations so that the Roles model could have a has_many association for Mentor and Student.

Alt Text

This would not have been a problem but this would have taken a lot longer to understand and figure out. The project had to be completed in a week. What's worse was that I did not know how to use these custom associations in my controllers. Additionally, my primary/foreign keys were not working (due to my lack of understanding) which made my nested resources break and my routes not be designed correctly.

So... I decided to SCRAP this project


The new project ended up being a website about an international violin competition called the Menuhin Competition 2021. This website would allow users to upload ongoing performances and blog about it.

Alt Text

The benefit of these associations is that I can have a many-to-many association.

For example:

  • Users has many performances they can upload but each uploaded performance belongs to a user
  • A user can comment on any uploaded performance. Because comments is the joins table, user has many performances through comments. -A user can post a blog. So each blog belongs to a user but a user can post many blogs.

For me, this new project ended up have far simpler associations that I could comprehend. But I still hope as I continue to try deepen my understanding on associations that I can come back to the original project and try complete it. For now, I learnt the hard way how important associations are.

Discussion (0)

Editor guide