Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
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
the way things are setup now, we might have a little more trouble looking up
the post that a comment belongs to.
So suppose that all we had was a comment and
we wanted to get the post that it belongs to.
We can say comment = Comment.last.
Notice the post_id: 7 on the end there.
That's from the foreign key column we added to the comments table.
It's pretty easy to use that to do something
And there's Post id number 7 at the bottom.
But it could be even easier.
After all, we can get a post comments using a single method call like,
Could we do something similar to get the post that belongs to a comment?
Not at this point.
If we try to call comment.post, we'll get NoMethodError: undefined method `post'.
The only reason that we have a comments method on our post objects is that we
added the has many comments association in the post model class.
If we want a post method on our comment objects,
we need to add a similar association in our comment class.
So we'll go to app > models > comment.rb, and
we'll add the association belongs_to :post.
we use the symbol :post in all lower case instead of the name of the post class.
Let's save this and go back to our console.
And don't forget, we need to exit the console and
then restart it with bin/rails c.
That'll reload our code changes.
Now we can load up a comment, comment = Comment.last.
And we can access the post it belongs to by calling its new post method,
We already made all the database changes we needed when adding the has many
comments association to the post class.
So our belongs to post association works immediately.
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.
You need to sign up for Treehouse in order to download course files.Sign up