Introduction2:52 with Jay McGavren
Our blog app doesn't allow users to tell us how awesome our posts are. This situation is, of course, unacceptable. So we're going to need a comments system. We need to be able to associate comments with a particular post.
[MUSIC] 0:00 Our blog app doesn't allow users to tell us how awesome our posts are. 0:04 This situation is of course unacceptable. 0:08 So we're going to need a comment system. 0:12 But this introduces a problem. 0:14 How do we store the comments in the database? 0:17 Do we add additional columns to the post table to hold them? 0:19 How many do we add? 0:22 If we add just five columns for 0:24 comments, any given post will be able to have a maximum of five comments. 0:25 If we add 50 columns, most of those columns will sit empty most of the time. 0:30 We could create a separate comments table where each record holds one comment. 0:34 Then we could have as many or as few comments as we want. 0:38 But then how do we show that comments belong to a particular post? 0:41 Do we just show a big lump of comments on the front page 0:45 that belong to the blog as a whole? 0:48 That doesn't sound very useful. 0:50 What we need to do is associate comments with a particular post. 0:52 We can do this by adding a column to the comments table that refers 0:57 back to particular records in the post table. 1:00 All the records in comments that have a value of 1 in this new post ID 1:03 column will refer back to the record with an ID of 1 in the post table. 1:07 All the records in comments that have a post ID of 3 1:11 will refer to the record with an ID of 3 in the posts table. 1:14 All the records with post ID of 7 will refer to the post with an ID of 7 1:18 and so on. 1:22 This probably sounds like keeping track of all these IDs that associate records with 1:23 each other will be a huge pain. 1:28 Don't worry, the Active Record library that comes with Rails will 1:30 manage everything for you, automatically. 1:33 We just have to set up the tables and then tell Active Record 1:36 that the post model class is associated with a comment model class. 1:39 From then on, we can manage the creation, reading, updating, and 1:43 deleting of comments through methods on post objects. 1:47 And Active Record will handle all the messy database-related 1:51 stuff that associates the comment with the post. 1:54 Sounds cool, right? 1:58 This is yet another way that Rails can save you a lot of work. 1:59 In this course, we're going to show you how to configure and 2:02 use active record associations. 2:05 This course assumes that you've gone through all the prerequisites 2:08 listed on the course page. 2:11 You need to already be familiar with Rails models, views, controllers, and routes. 2:13 A bit of knowledge about SQL join queries will be helpful as well. 2:18 We've included links to resources where you can learn more in the teacher's notes. 2:22 Notes for later videos in the course will have links to other relevant information. 2:26 So if at any point you encounter something you don't understand, 2:30 check the teacher's notes, the solution is probably there. 2:33 And don't hesitate to use the video playback controls if you need to. 2:38 You can adjust the playback speed to be faster or 2:42 slower, depending on how you want to view the current content. 2:44 Okay, ready? 2:48 Let's set up your first Active Record Association. 2:49
You need to sign up for Treehouse in order to download course files.Sign up