Hello and welcome.
In this lecture we are going to associate our models.
We have got two models at the moment for our blog application.
We want the models to have a relationship and be able to reference each other.
I want us to take a look at the app model's comments.
Are be so feig inside it at their entry models and then comments.
So if you look at it here this is similar to the post.
Requote here.
This post model.
Okay.
So there's a slight difference the only the key difference here is this belongs to post.
So what exactly.
What that actually means is that the active record associations reference the active record Association
the reference key word used in that when we created the model we used the key word reference.
Let me see if we have to quote that secret here.
So.
Yeah.
So when the model was created here we use this key word references.
I mean we are referencing this post model.
So that's what is making references to us here.
That's way same belongs to underscore to his referencing the older model.
What that does what it it creates this new column in the database table in a week.
They provide a model name and then he spends it with an underscore.
And then the idae on this enables it to hold integer values that it stores values that and numbers so
the active record Association busily.
It allows you to easily declare the relationship between the two modules.
So in this case the models are comments and pause.
So when you if you were to write out the relationship the way you specify that you want each you want
each comment to belong to one post.
I knew was all want one paused for one pause you want to be able to leave many comments and none of
that makes sense so you want what you want for your post for several people to be able to leave a comment.
And also you want for each comment one each comment to belong to one post so we can see here B association
here.
So what we need to do now we need to go to the post model which is this one here on the net and we need
to just tab down and add a reference because we want one post to have to make it possible for you to
form one post will have many comments.
Several people should be able to make comments on one post.
If the post is popular you want several people to be able to make comments to it if they wish to.
So to do that we need to add a reference to this model so all we need to do is type in has.
On the score.
Many.
Space Corps long.
Comments.
So that's all we need to write.
And that when they ball many people to leave comment upon an individual post does the file.
And see all saw by Add in.
That enables you for example if you have an instance variable of the POWs variable that contains a post.
You should be able to retrieve all the comments belonging to that post by eating.
Look true or used that as an array.
For example you can use post comments you see or the commas linked to all an individual post.
Next one of do we want to implement routs for our comit we don't want to manually do that.
So like we did with the post we Soulsby going to add a resource.
So what we need to do now is look for word in config directory.
Expand that and look at the routs So inside the raoult we are going to specify a resource for the comment.
So where we've got ressource here quad post we're going to come down here and do another resource for
comments in the typing resource.
K.
So face call on.
Comments.
Resources are for God the s.
Just one I forgot to add.
Yes.
So we're just going to mend this racehorse slightly.
We're going to school in a poor space and type in the word do and we are going to nest.
We're going to condemn that and nest that on there.
We going to put an end.
Tag end state men there.
So basically what I've done here I nest in this inside the post comment.
All right so yours is go.
Is this dead as a resource within the post.
That's why I've done it this way.
So make sure you've got your changes saved and save or what we need to do now is look for just type
in the command and check what rules.
Ha.
Now that we've added the comment resources we should have extra routs.
So let me just clear mine cons..
So typing the command that would check the raoult SEE Do been slash Real's space routs.
And that should display all the routs he should and the new rantz for the comments.
So there you go.
So is this Blaine all the rules for our application here.
These are the previous rules for the Post's resource or we just creep.
We just added resource for the comments so you can see or the resource here for the comments or Rice
and the index because the leaves are or nested.
You can see there's nested.
Okay.
So we can see or there answer here we can see one here for New York Post on the school comment and conceived
posts.
Gives you the the pass with poor slash post on the score in the comments.
And as for new comments this just tells you how it is nested that where where basically it is nested
inside this post ressource.
So that's it for this lecture in the next election.
We aren't going to generate a controller for the comments.
Thanks for watching.