1 00:00:00,600 --> 00:00:01,520 Hello and welcome. 2 00:00:01,520 --> 00:00:07,780 In this lecture we are going to create the model for our post at the moment. 3 00:00:07,800 --> 00:00:13,500 We are not able to submit the form to a database. 4 00:00:13,530 --> 00:00:17,660 So by creating a model to enable us to do that. 5 00:00:18,000 --> 00:00:21,540 So this is what our form of mission looks like at the moment. 6 00:00:21,580 --> 00:00:28,410 At the moment we're with some in the form is just going to render the in parameters on the screen as 7 00:00:28,410 --> 00:00:29,730 we can see here. 8 00:00:30,090 --> 00:00:39,150 So all these different database that comes with reals is askew a light you can use at any database you 9 00:00:39,150 --> 00:00:47,280 want and does really matter obscure serve my school posek cress any database will work the same way. 10 00:00:47,520 --> 00:00:53,940 Let's have a look at where database settings are defined in this application. 11 00:00:53,940 --> 00:00:58,820 So as usual he will be in the configuration fast. 12 00:00:59,340 --> 00:01:07,620 So if we look down here there's a config directory and inside this conflict derrek FREE there's a database 13 00:01:07,660 --> 00:01:09,190 doorway air fast. 14 00:01:09,210 --> 00:01:15,120 This is where the database settings are defined you can see here's his default. 15 00:01:15,120 --> 00:01:22,520 You've got a default adapter which is priscu a light if you were in it tells you he also database the 16 00:01:22,600 --> 00:01:28,740 be obscure light in development so if he's in my secure for example this is where you would define if 17 00:01:28,740 --> 00:01:36,510 you were to use my secure database you would first have to install databases on your development machine 18 00:01:37,140 --> 00:01:45,930 and then you have to come in here and change the settings to reflect what database you are using because 19 00:01:45,930 --> 00:01:49,310 we're going to use a default or the settings is already in place. 20 00:01:49,320 --> 00:01:57,750 We don't need to change anything or open up your terminal or command line so we can create a model for 21 00:01:57,750 --> 00:01:58,830 the post. 22 00:01:58,830 --> 00:02:04,630 So if you're using a badge like me if you want to colour your screen you're just typing Claire. 23 00:02:05,790 --> 00:02:07,550 And that should clear your screen. 24 00:02:07,560 --> 00:02:13,140 So what we're going to do now is actually going to generate a model. 25 00:02:13,140 --> 00:02:17,540 It is a model that will interface with the database. 26 00:02:17,840 --> 00:02:19,280 So it's a great model. 27 00:02:19,380 --> 00:02:26,040 Just type in Been slash rails slash. 28 00:02:26,250 --> 00:02:35,310 You can either type in the forward generate or just type in G generate or you can type in G for short 29 00:02:35,340 --> 00:02:36,780 to do the same thing. 30 00:02:36,840 --> 00:02:39,030 They need to specify what you are generating. 31 00:02:39,030 --> 00:02:42,500 You say a model and then you. 32 00:02:42,630 --> 00:02:48,060 When you're creating models models are single in compare the two. 33 00:02:48,060 --> 00:02:52,180 When you are creating controllers controllers are plural. 34 00:02:52,200 --> 00:02:56,140 So how about creating a model called POLST. 35 00:02:56,690 --> 00:03:04,020 Moses I think the post then you need to specify the field for the model for the field in the database 36 00:03:04,230 --> 00:03:12,990 or the way you generate a model you give it a name that is singler but the corresponding tables that 37 00:03:13,050 --> 00:03:17,850 is created can be plural will be plural. 38 00:03:17,850 --> 00:03:21,650 So we need to define the fields for the module. 39 00:03:21,810 --> 00:03:28,860 So he's going to have a title so I typed in title and they put in the quoll on the title is going to 40 00:03:28,860 --> 00:03:32,100 be is freeing data type. 41 00:03:32,350 --> 00:03:41,810 Okay do a space is also going to have a body and do a call on the body is going to be a text. 42 00:03:42,480 --> 00:03:44,940 So that is basically it. 43 00:03:44,940 --> 00:03:47,850 What we need to generate the model. 44 00:03:48,240 --> 00:03:54,200 So what this command is saying that we are telling reals that we want. 45 00:03:54,330 --> 00:04:04,020 He pulls to model together with their title attribute of the types shrink and a body attribute of the 46 00:04:04,020 --> 00:04:05,470 type text. 47 00:04:05,680 --> 00:04:15,090 So these attributes are automatically added to the post table in the database and they will be mapped 48 00:04:15,540 --> 00:04:17,400 to the portrait model. 49 00:04:17,430 --> 00:04:18,540 So press enter. 50 00:04:18,630 --> 00:04:19,720 And that's huge. 51 00:04:19,770 --> 00:04:30,280 Generate the model so reals has kindly responded to the request and is created a bunch of files here. 52 00:04:30,440 --> 00:04:40,110 The ones we are the only one we interested in here is it up model slash post derby which is this one 53 00:04:40,110 --> 00:04:41,910 here. 54 00:04:42,260 --> 00:04:52,490 This one here this up models last post not hard and this one here we're interested in this too. 55 00:04:52,830 --> 00:04:54,680 That one and that one. 56 00:04:54,840 --> 00:04:59,470 So when you generate it model it. 57 00:04:59,690 --> 00:05:01,860 But it's this far. 58 00:05:02,040 --> 00:05:03,850 Okay so the father there. 59 00:05:03,870 --> 00:05:12,930 But you need to run the migration for the database tables to be created a list of table not being created. 60 00:05:13,020 --> 00:05:19,110 So you just have to go in and run the command that will create the table but we can take a look before 61 00:05:19,110 --> 00:05:25,390 we run the migrates command active record which is this here. 62 00:05:25,540 --> 00:05:29,810 This active work on the active record is quite smart. 63 00:05:29,870 --> 00:05:35,840 It will automatically map mccollom names to the module attributes. 64 00:05:36,120 --> 00:05:42,170 Okay so the attributes were given in model the active recall automatically manacle on names to a do 65 00:05:42,190 --> 00:05:49,890 module attributes which means you don't have to declare that Ruby was inside the reals model as that 66 00:05:49,890 --> 00:05:57,690 will be done automatically for you by the active record so to look at the file those create it where 67 00:05:57,750 --> 00:05:59,470 we generated the model. 68 00:05:59,610 --> 00:06:02,880 So we come here and look at. 69 00:06:02,880 --> 00:06:06,030 This should be an option for the B. 70 00:06:06,050 --> 00:06:12,030 Look inside it dbe directory expand that it tells you anywhere where you create the model it tells you 71 00:06:12,030 --> 00:06:14,460 where it tells you the part. 72 00:06:14,560 --> 00:06:24,270 Our models Polestar B and here dbe migrants so we're looking at this one now dbe migrate which is here 73 00:06:24,390 --> 00:06:25,720 which is this one here. 74 00:06:25,920 --> 00:06:28,100 So basically this file here. 75 00:06:28,110 --> 00:06:30,120 This is why the active record discrete. 76 00:06:30,330 --> 00:06:37,060 This is a table that's going to be create it's going to create a table quad post which is plural and 77 00:06:37,290 --> 00:06:39,490 the table will have this with this field. 78 00:06:39,510 --> 00:06:42,040 You have a title and a body. 79 00:06:42,060 --> 00:06:50,010 It also gives you a timestamp So when you generate or when you run the migration it all include a time 80 00:06:50,010 --> 00:06:55,010 stamp which means you can always revert back if you look here. 81 00:06:55,330 --> 00:07:02,670 You see the date this has the time stamp so when you are looking for or wrong migration it all generate 82 00:07:02,790 --> 00:07:03,820 a time stamp. 83 00:07:03,820 --> 00:07:06,790 You can use to reference back as it is now. 84 00:07:06,810 --> 00:07:07,750 This for us. 85 00:07:07,760 --> 00:07:08,960 To run this. 86 00:07:09,000 --> 00:07:16,800 This is an active migration we need to run this for the table to be created so all we need to do is 87 00:07:16,800 --> 00:07:31,040 come into this command line here and we type in being slas braille's space need be migrate. 88 00:07:33,670 --> 00:07:34,750 Press Centre. 89 00:07:35,180 --> 00:07:44,880 And that will generate a migration that will create this table quote post with the title body and you 90 00:07:44,910 --> 00:07:52,740 have two fields quote one will be Title One will be body so you can see the migration has been executed. 91 00:07:52,900 --> 00:07:56,500 You can tell us here here that the what. 92 00:07:56,530 --> 00:08:04,380 It is my great head that it created the table called post in this time and this is the. 93 00:08:04,540 --> 00:08:11,080 This is when it was migrated so it give you a time stamp the time stamp enables you to roll back out 94 00:08:11,110 --> 00:08:16,710 if you've had if you've done several migrations you can easily tell which ones you've you. 95 00:08:16,750 --> 00:08:22,470 By the time stamp one are referred back so we now have our table created. 96 00:08:22,480 --> 00:08:26,380 You can see the migration has been executed successfully. 97 00:08:26,530 --> 00:08:30,740 So that's it for this collection now that we've caught our model created. 98 00:08:30,760 --> 00:08:39,010 We can now communicate with the database in our next lecture or we are going to see how we can see data 99 00:08:39,430 --> 00:08:40,720 in the controller. 100 00:08:40,990 --> 00:08:41,960 Thanks for watching. 101 00:08:42,040 --> 00:08:50,110 And by phone again sorry this show again has gone a long time to you know do as much as possible to 102 00:08:50,110 --> 00:08:54,970 explain it the best way you can so that you can get it too if you do have any issues. 103 00:08:54,970 --> 00:08:56,040 Please let me know. 104 00:08:56,050 --> 00:08:56,890 Thanks for watching. 105 00:08:56,920 --> 00:08:57,830 And by follow.