In the last section we started working on dropping each of the collections that we've created between
every single test and we ended the section by saying gee this doesn't really look like a great approach
right here with this kind of Colback of Dune that we've got.
Well here's the downside.
Here's the bad thing about this.
Unfortunately you cannot draw up multiple collections inside of Mongo at the same time.
And so it kind of leads us towards taking this like sequential approach where we draw up each collection
one at a time.
Now if you don't like this kind of callback of doom that we've got right here we could definitely refactor
this to loop over all the different collections we have.
I'll leave it up to you to do that refactor if it's something that you really want to see now really
important.
I did make one big typo in the last section Loxley too and we're going to fix both them up right now.
First when mongoose loads up these collections to Mongo D.B and they get reflected over and Mongo D-B
Mungo's actually normalizes each collection name by lower casing the entire collection name so blog
posts with a capital P is not correct.
It should actually be kept or blog posts with a lowercase P like so.
So I need to also make sure that I update this inside of the drop statement right here as well.
So that was a little bit of a typo.
I did make one of the typo I know.
Just awful.
Two in a row I put in the incorrect require statements in here.
So I said require source user.
This is not a correct path.
In fact we want to go up one directory in all three cases because right now we're inside of the test
directory.
We want to go up to users into source code and get the different model files.
So again two two typos in my on my side.
My mistake.
OK.
Hopefully you can forgive me.
So let's start working on some tests around associations.
So our entire goal right now is to create some user's comments and blog posts associate them all together
in some fashion and just kind of see how they work how they play together.
I'm going to expect that we might write a couple of different tests around these associations so I'm
going to create one user one comment and one blog post in before each and then we'll see how these all
worked together.
So let's start off by defining R before each
as is definitely going to be an asynchronous before each.
Some definitely go in to include a done callback inside of here.
We're also going to create three different variables inside of here that I want to use inside of all
of the different it blocks side put inside.
This described block so I'm going to initialize these three variables at a time.
I remember that this initialization is all about how a scope works inside a javascript.
Since we're going to want to assign these variables inside that for each we need to make sure they're
inside the outer scope as well.
It's going to find Joe a blog post and a comment.
Do make extra sure that you've got lowercase b blog post and lowercase c.
Remember that up top the Capitol versions of all these models indicate that this is the model class
whereas lower case indicates that this is an instance of that model.
OK so Joe blog post in comment.
Now we are going to create one of each of these user blog posts and comments.
So say Joe is a new user with the name of Joe.
And notice they are not going to set up any associations just yet.
I will then save blog post is New Blog Post.
It's going to have a title of jazz is great.
And how about some content of the show.
It really is.
Or something like that.
Then for the comment we'll make a new comment with some amount of content contrats on great post.
OK.
That's pretty much it for three Set-Up resources.
So I mean I'm out for just one second so you can see the entire line on each one.
Let's go back in.
So again you'll notice that I did not do anything that really makes it appear like these records are
associated in any such fashion.
So I've not done anything to make it seem like Joe owns this blog post or that this comment belongs
to this blog post or that Joe made the comment itself.
So like nothing is tied between here just yet to four of these associations we have to do so in a very
direct fashion.
So this is like just very direct We are setting up these associations in a very direct manner.
We'll first start off by associating Joe with the blog post.
So we're going to assume that Joe authored this blog post or that this blog post belongs to him.
I want to flip over to our user file really quick and remind you that blog post right here.
A user can have a collection of blog posts.
So this is in array right here.
They can have a collection of blog posts so to associate a blog post with a user.
We can write Joe dog blog posts dot push blog post and that's it.
You're going to look at this and immediately think to yourself Wow this looks a lot like document nesting.
Well yes very similar very similar.
So we say Joe has a collection of blog posts.
I want him to have a new association to this blog post that we just created right here.
And so I'm going to push that entire blog post in there.
You'll notice very importantly this is a little bit of magic on the mongoose side over on blog posts.
We had said that this was going to be of type Object ID but we just put in an entire model here.
So this is a little bit of magic on mongooses side.
Mongoose is going to recognize that we just push in an entire model and it's going to automatically
say OK they're probably trying to set up an association here.
So I'm not going to mess with the entire model itself.
I'm just going to set up this object I.D. type to refer to this other blog post.
So much of this is going to just automatically do the right thing for us even though we pushed in the
entire model.
So now we can set up an association between a blog post and the comment in a very similar fashion.
So say Blog Post dot comments.
Push comment.
So again a blog post has many comments.
So if we open up our blog post schema there it is right here.
Comments is an array because it's an array.
We are going to push in this comment model to it.
Mangus is going to work the exact same way here.
It's going to say OK comments should be recording object IDs and they just attempted to push in a whole
model.
Chances are they want to store the whole model they just want a reference to the object ID.
So again mongoose saving the day for us.
OK.
Last thing.
Last one.
In this case we want to associate Joe with this particular comment.
So this time around it's a has one relationship.
So a comment.
How does one user that is Joe.
So this time around we are not pushing into an array of users who want to just say this comment has
exactly one author and it is Joe.
So to set up this relationship we say comment dumb user because Joe All right now this one right here
absolutely magic as well.
Total magic on mongooses side because you know you can kind of Except that push is like going to look
at this thing and say OK it's a model.
Yeah there's some magic going on whatever it's taking the object ID.
But in this case right here we are like no two ways about it.
We're saying this user property is equal to Joe.
So mongoose clearly has a center going on behind the scenes or some type of interception on this assignment
right here and it's going to say they are trying to assign Joe to this property but this is supposed
to be a reference.
So chances are they don't want to set like all of Joe equal to saying they just want to have a reference
to Jila signed.
So again big piece of magic on mongooses side.
All right.
So at the risk of this turning into a really long section let's take a quick break and then we're going
to figure out how to save all these records because they've not been saved to our database yet.
So to take a quick break figure out how we saved them and then work on an assertion.