Belongs To Associations2:20 with Jay McGavren
We can easily look up the Comments that belong to a Post. But the way things are set up now, we might have a little more trouble looking up the Post that a Comment belongs to. The belongs_to association can fix that.
We can easily look up the Comments that belong to a Post. But the way things are set up now, we might have a little more trouble looking up the Post that a Comment belongs to.
- Suppose all we had was a comment, and we wanted to get the
Postthat it belongs to.
- Go to
app/models/comment.rb, and add:
- Now we can load up a
comment = Comment.last, and access the
Postit belongs to by calling its new
comment.post. We already made all the database changes we needed when adding the
has_many :commentsassociation to the
Postclass, so our
belongs_to :postassociation works immediately.
- Go to
Now you know how to set up
belongs_to associations. Anytime you set up a
has_many association from your first model to a second model, you're going to want to set up a
belongs_to association from the second model back to the first.
We can easily look up the comments that belong to a post, but 0:00 the way things are setup now, we might have a little more trouble looking up 0:03 the post that a comment belongs to. 0:06 So suppose that all we had was a comment and 0:09 we wanted to get the post that it belongs to. 0:11 We can say comment = Comment.last. 0:13 Notice the post_id: 7 on the end there. 0:19 That's from the foreign key column we added to the comments table. 0:23 It's pretty easy to use that to do something 0:27 like Post.find(comment.post_id). 0:32 And there's Post id number 7 at the bottom. 0:38 But it could be even easier. 0:41 After all, we can get a post comments using a single method call like, 0:43 Post.last.comments. 0:48 Could we do something similar to get the post that belongs to a comment? 0:50 Not at this point. 0:54 If we try to call comment.post, we'll get NoMethodError: undefined method `post'. 0:55 The only reason that we have a comments method on our post objects is that we 1:03 added the has many comments association in the post model class. 1:07 If we want a post method on our comment objects, 1:13 we need to add a similar association in our comment class. 1:16 So we'll go to app > models > comment.rb, and 1:20 we'll add the association belongs_to :post. 1:24 Again, and 1:28 we use the symbol :post in all lower case instead of the name of the post class. 1:29 Let's save this and go back to our console. 1:33 And don't forget, we need to exit the console and 1:36 then restart it with bin/rails c. 1:39 That'll reload our code changes. 1:42 Now we can load up a comment, comment = Comment.last. 1:44 And we can access the post it belongs to by calling its new post method, 1:51 comment.post. 1:54 We already made all the database changes we needed when adding the has many 1:58 comments association to the post class. 2:02 So our belongs to post association works immediately. 2:04 Now you know how to set up belongs to associations. 2:08 Anytime you set up a has many association from your first model to a second model, 2:11 you're going to want to set up a belongs to association from the second model 2:15 back to the first. 2:19
You need to sign up for Treehouse in order to download course files.Sign up