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