DEV Community

Jessica Joseph
Jessica Joseph

Posted on • Edited on

2

Understanding :source on a has_many_through Relationship

As you know, associations are a crucial part of any Rails project in order to get your program to start functioning. I recently created a Ruby on Rails application called "Five Star Movies". A user can signup and review movies, as well as leave a review on other users movie reviews. While creating this application I was introduced to the concept of using :source on a has_many through: relationship. This was something I've never used before and had to familiarize myself with, so I figured I'd walk you through my thought process with me!

Setting Up My Associations

One of the firsts steps you should take when creating a Rails project is figuring how your models interact with one another and setting up your associations. In my application I had 4 models; user, movie, review, and genre. Each of my associations are as follows:

Movie-

belongs_to :user
belongs_to :genre
has_many :reviews
has_many :users, through: :reviews

Genre-

has_many :movies
has_many :users, through: :movies

Review-

belongs_to :user
belongs_to :movie

User-

has_many :movies
has_many :reviews
has_many :reviewed_movies, through: :reviews, source: :movie
has_many :genres, through: :movies

If you look close enough, you'll see that one of those associations are not like the rest. As you can see, in the users model, there is an association called :reviewed_movies. Again taking a closer look at it you will also see in that association is something called :source. It is also important to note that the movie model is singular in source, this is the basic syntax for writing this code.

Deciding When to Use has_many_through with :source

In my Users model when setting up my associations I have a has_many :reviews and a has_many :reviewed_movies, through: :reviews, source: :movie. Before deciding to use :source the second association above originally looked like; has_many :movies, through: :reviews. As you can see before changing the code, the two associations for the through model are both looking to the Movie model, which can get confusing and may run you into problems. To avoid this I implemented :source. Using source allows you to specifically name your associations while still being able to correctly grab the data from the proper model.

Importance of Implementing :source in this Application

Now that we established why the associations are set up as followed, taking a further dive into it, I wanted my users to be able to leave a review on other users movies as well as have access to all the reviews left by other users. The join table in my application is the Review model, so setting up my association like so allows the Users model to have access to the Movie model to grab all the movies with reviews left on it. This was important because it allows users to interact and see other one anothers reviews on the movies, which in turn allows for a better user experience. As you can probably see by now, using :source in my has_many through: association allowed for my application to become more dynamic and gives users access to more data.

Wrapping it Up!

Using :source in your Rails application allows for the customizable naming of associations. While creating this application, researching this concept made it possible for me to be able to get my app to behave exactly how I intended for it to. This concept(a very useful one I might add!) not only improved my app, but also helped improve my skills as a developer. It is definitely something I will be using again in the future as I create more Rails applications.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay