Start a free Courses trial
to watch this video
In this Treehouse Quick Tip, Jason demystifies the concepts behind what a has many :through association is in Ruby on Rails. We'll figure out what a join table is, how different models fit together, and the syntax needed to make it all work. Score one for Treehouse!
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up[Treehouseβ’: presents] 0:00 [Quick Tips: What is a has_many :through association in Ruby on Rails with Jason Seifer] 0:02 Hi! I'm Jason. 0:05 In this Treehouse Quick Tip, 0:06 we're going to talk about what a has_many through association is 0:08 in Ruby on Rails. 0:11 A has_many through association starts with a has_many association. 0:13 As an example, let's say that we're a magazine publisher 0:18 building out our website. 0:21 We have a model for magazines and for users. 0:24 We want to be able to show a user a list of magazines 0:27 that they are subscribed to. 0:30 We can do this by using something called a join table. 0:32 A join table will keep track of the first model--in this case, the user, 0:36 and the second model, which in this case is the magazine. 0:40 But we can call our join model a subscription. 0:44 Now, let's take a look at what the code looks like. 0:47 So here's a user class and a magazine class. 0:51 What we want to do is also create a subscription class 0:55 which is going to be our join table. 0:58 So let's say that we generated a subscription class. 1:01 It would look something like this. 1:03 Now, on the back end when we were creating our subscription class, 1:12 the table would look something like this. 1:17 In addition to an ID column, it would have two fields. 1:20 One for the user ID and one for the magazine ID. 1:24 So we're going to go ahead and pretend that that's how it is right now. 1:30 If we wanted to set all of that up as a relationship, 1:34 we could say that a user has many subscriptions. 1:37 And we would also say that a magazine has many subscriptions. 1:43 So then we set up the subscription class to say that it belongs to 1:52 a user and it belongs to a magazine as well. 1:56 Now that we have these associations set up, 2:05 we can say that our user has many magazines 2:08 through the subscriptions relationship. 2:15 And we can also do the same thing with our magazine class. 2:20 Well, how would you use something like this? 2:30 Well, let's say that you we're trying to find the user. 2:33 Grab the first user, 2:37 and you also grab the first magazine. 2:41 If we wanted to create a subscription, 2:44 we could say something like user.subscriptions.create. 2:46 And then we tell it that the magazine, 2:53 which is an association up here on subscription, 2:55 is this magazine that we just found. 2:59 Now what we can do, if we really want to, is call user.magazines, 3:03 and that would return our magazine. 3:10 The same thing can apply to models that refer back to themselves. 3:14 For example, let's take a site like Twitter 3:18 and apply the same model of subscriptions but to users and followers. 3:21 Followers are just another class of users, 3:26 and here's what the code for that looks like. 3:29 Now, you might be wondering what we would do 3:32 if we wanted to have a has_many through association 3:33 where the other end of the has_many through association 3:36 is the same model as the original. 3:40 Sort of like how you would do a Twitter site with users and followers. 3:43 Well, the way we would do that is like this. 3:48 Let's say we had a our user class 3:50 and then the join table that we we're going to use-- 3:53 the joining class is called followings. 3:56 So let's say we created a following class that had a user ID 4:01 and a follower ID. 4:03 Now, we could tell our user to say that it has_many followings. 4:11 And then our following class would belong to a user. 4:18 So this is just one part of the association. 4:23 In order to do the other part where we're going to have a follower as well, 4:27 we would say that it belongs to-- 4:32 we fix that right there-- 4:37 belongs to a follower, and the class name for that is user. 4:39 In order to specify that it points back to a class 4:47 other than what Rails usually infers, 4:49 you use the class_name hash key when specifying it belongs to association. 4:53 In this case, we're saying the class name is user. 4:58 So it's just going to point back to a different user. 5:02 Now, over on the user class, we say has_many followers, 5:06 which is this part of the association here, 5:14 through followings. 5:18 And that's how you do a has_many association 5:21 where the second part points back to the same kind of class. 5:24 Behind the scenes, Rails will look up all of the follower IDs at the table 5:29 and then do a separate query to the database 5:34 to bring back those users. 5:36 It's two database queries to get the information, 5:39 but it's an extremely useful technique. 5:41
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up