1
00:00:00,660 --> 00:00:01,600
Hello and welcome.

2
00:00:01,620 --> 00:00:06,890
In this lecture we are going to associate our models.

3
00:00:06,890 --> 00:00:10,930
We have got two models at the moment for our blog application.

4
00:00:10,950 --> 00:00:17,570
We want the models to have a relationship and be able to reference each other.

5
00:00:17,640 --> 00:00:24,020
I want us to take a look at the app model's comments.

6
00:00:24,190 --> 00:00:31,150
Are be so feig inside it at their entry models and then comments.

7
00:00:31,200 --> 00:00:38,470
So if you look at it here this is similar to the post.

8
00:00:39,460 --> 00:00:40,250
Requote here.

9
00:00:40,270 --> 00:00:41,660
This post model.

10
00:00:42,040 --> 00:00:42,470
Okay.

11
00:00:42,490 --> 00:00:49,170
So there's a slight difference the only the key difference here is this belongs to post.

12
00:00:49,720 --> 00:00:51,310
So what exactly.

13
00:00:51,370 --> 00:01:02,980
What that actually means is that the active record associations reference the active record Association

14
00:01:03,580 --> 00:01:13,180
the reference key word used in that when we created the model we used the key word reference.

15
00:01:13,180 --> 00:01:18,080
Let me see if we have to quote that secret here.

16
00:01:18,100 --> 00:01:18,730
So.

17
00:01:18,770 --> 00:01:19,230
Yeah.

18
00:01:19,420 --> 00:01:24,440
So when the model was created here we use this key word references.

19
00:01:24,460 --> 00:01:28,370
I mean we are referencing this post model.

20
00:01:28,570 --> 00:01:30,950
So that's what is making references to us here.

21
00:01:30,970 --> 00:01:37,710
That's way same belongs to underscore to his referencing the older model.

22
00:01:37,790 --> 00:01:46,880
What that does what it it creates this new column in the database table in a week.

23
00:01:46,900 --> 00:01:52,070
They provide a model name and then he spends it with an underscore.

24
00:01:52,090 --> 00:02:01,540
And then the idae on this enables it to hold integer values that it stores values that and numbers so

25
00:02:01,560 --> 00:02:05,370
the active record Association busily.

26
00:02:05,380 --> 00:02:11,450
It allows you to easily declare the relationship between the two modules.

27
00:02:11,470 --> 00:02:16,440
So in this case the models are comments and pause.

28
00:02:16,840 --> 00:02:22,990
So when you if you were to write out the relationship the way you specify that you want each you want

29
00:02:23,050 --> 00:02:27,410
each comment to belong to one post.

30
00:02:27,580 --> 00:02:35,830
I knew was all want one paused for one pause you want to be able to leave many comments and none of

31
00:02:35,830 --> 00:02:42,980
that makes sense so you want what you want for your post for several people to be able to leave a comment.

32
00:02:43,280 --> 00:02:54,100
And also you want for each comment one each comment to belong to one post so we can see here B association

33
00:02:54,130 --> 00:02:54,750
here.

34
00:02:55,000 --> 00:03:01,990
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

35
00:03:02,080 --> 00:03:15,070
to just tab down and add a reference because we want one post to have to make it possible for you to

36
00:03:15,430 --> 00:03:18,000
form one post will have many comments.

37
00:03:18,130 --> 00:03:22,090
Several people should be able to make comments on one post.

38
00:03:22,090 --> 00:03:27,920
If the post is popular you want several people to be able to make comments to it if they wish to.

39
00:03:28,250 --> 00:03:35,670
So to do that we need to add a reference to this model so all we need to do is type in has.

40
00:03:36,720 --> 00:03:38,830
On the score.

41
00:03:39,600 --> 00:03:40,520
Many.

42
00:03:41,770 --> 00:03:44,490
Space Corps long.

43
00:03:44,950 --> 00:03:46,160
Comments.

44
00:03:48,800 --> 00:03:50,420
So that's all we need to write.

45
00:03:50,600 --> 00:03:58,680
And that when they ball many people to leave comment upon an individual post does the file.

46
00:03:59,550 --> 00:04:03,240
And see all saw by Add in.

47
00:04:03,240 --> 00:04:14,110
That enables you for example if you have an instance variable of the POWs variable that contains a post.

48
00:04:14,190 --> 00:04:21,150
You should be able to retrieve all the comments belonging to that post by eating.

49
00:04:21,180 --> 00:04:24,310
Look true or used that as an array.

50
00:04:24,330 --> 00:04:32,140
For example you can use post comments you see or the commas linked to all an individual post.

51
00:04:32,130 --> 00:04:38,150
Next one of do we want to implement routs for our comit we don't want to manually do that.

52
00:04:38,160 --> 00:04:43,770
So like we did with the post we Soulsby going to add a resource.

53
00:04:43,770 --> 00:04:48,830
So what we need to do now is look for word in config directory.

54
00:04:49,020 --> 00:04:59,080
Expand that and look at the routs So inside the raoult we are going to specify a resource for the comment.

55
00:04:59,160 --> 00:05:08,280
So where we've got ressource here quad post we're going to come down here and do another resource for

56
00:05:08,400 --> 00:05:11,790
comments in the typing resource.

57
00:05:13,790 --> 00:05:14,740
K.

58
00:05:15,040 --> 00:05:18,330
So face call on.

59
00:05:21,230 --> 00:05:22,500
Comments.

60
00:05:22,880 --> 00:05:25,150
Resources are for God the s.

61
00:05:27,780 --> 00:05:29,500
Just one I forgot to add.

62
00:05:29,500 --> 00:05:30,980
Yes.

63
00:05:31,250 --> 00:05:34,470
So we're just going to mend this racehorse slightly.

64
00:05:34,470 --> 00:05:41,120
We're going to school in a poor space and type in the word do and we are going to nest.

65
00:05:41,700 --> 00:05:46,240
We're going to condemn that and nest that on there.

66
00:05:46,300 --> 00:05:49,610
We going to put an end.

67
00:05:51,850 --> 00:05:54,450
Tag end state men there.

68
00:05:54,520 --> 00:06:02,550
So basically what I've done here I nest in this inside the post comment.

69
00:06:02,560 --> 00:06:04,270
All right so yours is go.

70
00:06:04,270 --> 00:06:08,790
Is this dead as a resource within the post.

71
00:06:08,950 --> 00:06:10,750
That's why I've done it this way.

72
00:06:12,440 --> 00:06:20,810
So make sure you've got your changes saved and save or what we need to do now is look for just type

73
00:06:20,810 --> 00:06:23,550
in the command and check what rules.

74
00:06:23,600 --> 00:06:24,170
Ha.

75
00:06:24,500 --> 00:06:30,350
Now that we've added the comment resources we should have extra routs.

76
00:06:30,420 --> 00:06:34,100
So let me just clear mine cons..

77
00:06:36,030 --> 00:06:50,010
So typing the command that would check the raoult SEE Do been slash Real's space routs.

78
00:06:51,030 --> 00:06:58,020
And that should display all the routs he should and the new rantz for the comments.

79
00:07:00,820 --> 00:07:01,600
So there you go.

80
00:07:01,600 --> 00:07:06,370
So is this Blaine all the rules for our application here.

81
00:07:06,370 --> 00:07:11,670
These are the previous rules for the Post's resource or we just creep.

82
00:07:11,680 --> 00:07:20,380
We just added resource for the comments so you can see or the resource here for the comments or Rice

83
00:07:20,410 --> 00:07:24,970
and the index because the leaves are or nested.

84
00:07:24,970 --> 00:07:26,870
You can see there's nested.

85
00:07:27,220 --> 00:07:28,330
Okay.

86
00:07:29,350 --> 00:07:37,190
So we can see or there answer here we can see one here for New York Post on the school comment and conceived

87
00:07:37,200 --> 00:07:38,110
posts.

88
00:07:38,150 --> 00:07:44,150
Gives you the the pass with poor slash post on the score in the comments.

89
00:07:44,290 --> 00:07:53,230
And as for new comments this just tells you how it is nested that where where basically it is nested

90
00:07:53,260 --> 00:07:56,500
inside this post ressource.

91
00:07:56,680 --> 00:07:59,290
So that's it for this lecture in the next election.

92
00:07:59,290 --> 00:08:04,210
We aren't going to generate a controller for the comments.

93
00:08:04,210 --> 00:08:05,400
Thanks for watching.
