1 00:00:01,050 --> 00:00:04,220 Rails uses several components to take a request from the browser and 2 00:00:04,220 --> 00:00:05,690 return a response. 3 00:00:05,690 --> 00:00:08,910 If any of these components is missing or configured incorrectly, 4 00:00:08,910 --> 00:00:10,710 your app won't work. 5 00:00:10,710 --> 00:00:13,770 We're going to look at a few situations where components are missing and 6 00:00:13,770 --> 00:00:15,370 show you how to fix them. 7 00:00:15,370 --> 00:00:17,678 To handle requests, Rails needs several things. 8 00:00:17,678 --> 00:00:19,873 First, an up-to-date database table. 9 00:00:19,873 --> 00:00:23,296 If the table doesn't exist or the data is in the wrong columns, 10 00:00:23,296 --> 00:00:26,780 Rails won't retrieve your model successfully. 11 00:00:26,780 --> 00:00:28,980 Second, a route telling it which controller and 12 00:00:28,980 --> 00:00:32,400 which action should handle requests for a given URL. 13 00:00:32,400 --> 00:00:35,800 And third, a controller action to actually handle the request. 14 00:00:37,280 --> 00:00:40,520 Let's look at a situation where the database is out of sync with our app 15 00:00:40,520 --> 00:00:42,680 code and see how to fix it. 16 00:00:42,680 --> 00:00:46,340 Our vet app tracks pets, but not the owners to which they belong. 17 00:00:46,340 --> 00:00:47,640 Let's create an owner model now. 18 00:00:48,650 --> 00:00:57,250 So we'll say rails generate model with a name of Owner, capital O. 19 00:00:58,640 --> 00:01:02,310 And we'll give it just one attribute, a name which is of type string. 20 00:01:04,380 --> 00:01:10,040 So rails just created an Owner model here in app/models/owner.rb, 21 00:01:10,040 --> 00:01:15,145 but we're a long way from being able to view owners in a browser. 22 00:01:15,145 --> 00:01:20,001 If we try to go to localhost port 3000 /owners right now, 23 00:01:20,001 --> 00:01:22,790 whoops, I forgot to start my server. 24 00:01:22,790 --> 00:01:24,070 Let me do that real quick. 25 00:01:24,070 --> 00:01:25,890 bin/rails server. 26 00:01:29,250 --> 00:01:34,760 So if we try to go to the owners path right now, we get an error. 27 00:01:34,760 --> 00:01:37,820 It says there's a pending migration error. 28 00:01:37,820 --> 00:01:41,200 Let's see if we can solve this problem and get back on track. 29 00:01:41,200 --> 00:01:44,650 We can start by looking at the stack trace as we did before. 30 00:01:44,650 --> 00:01:47,920 But if we do, we'll see that the error is not in our app. 31 00:01:47,920 --> 00:01:50,090 Instead, it's within the Active Record library. 32 00:01:51,350 --> 00:01:54,650 So in this case, we don't want to try to open the file in our editor. 33 00:01:55,730 --> 00:01:59,330 Changing code in Active Record or Rails won't fix our current problem, but 34 00:01:59,330 --> 00:02:01,380 could create many new ones. 35 00:02:01,380 --> 00:02:03,090 So what do we do? 36 00:02:03,090 --> 00:02:06,730 Let's start by looking at the helpful error message Rails printed for us. 37 00:02:06,730 --> 00:02:09,130 It says, migrations are pending. 38 00:02:09,130 --> 00:02:16,160 To resolve this issue, run: bin/rails db:migrate in RAILS_ENV=development. 39 00:02:16,160 --> 00:02:18,370 Let's go to our terminal and try running that command. 40 00:02:24,330 --> 00:02:27,920 So I'll quit out of my Rails server and 41 00:02:27,920 --> 00:02:32,080 paste the command we copied from the browser here. 42 00:02:32,080 --> 00:02:35,960 I should mention that commands run against the development database by default. 43 00:02:35,960 --> 00:02:41,140 So you can drop the RAILS_ENV=development environment variable if you want to, but 44 00:02:41,140 --> 00:02:42,170 it doesn't hurt to leave it on. 45 00:02:44,200 --> 00:02:47,553 So we hit Enter to run the command, Rails runs the migration. 46 00:02:47,553 --> 00:02:49,970 And let's try running the server again. 47 00:02:52,450 --> 00:02:54,370 Reload the page, and 48 00:02:54,370 --> 00:02:58,760 now we get a different error, but at least the pending migration error is gone. 49 00:02:58,760 --> 00:03:00,140 So what happened? 50 00:03:00,140 --> 00:03:02,040 When we generated our Owner model, 51 00:03:02,040 --> 00:03:06,890 Rails also created a migration file in the db/migrate folder. 52 00:03:08,940 --> 00:03:10,100 Here it is. 53 00:03:10,100 --> 00:03:14,340 It has instructions to update the database to store our owner objects. 54 00:03:14,340 --> 00:03:15,860 But we haven't run it yet. 55 00:03:15,860 --> 00:03:20,530 So we have an Owner class in our Ruby code, but no owners table in our database. 56 00:03:20,530 --> 00:03:24,780 When your code doesn't match the structure of your database, bad things can happen. 57 00:03:24,780 --> 00:03:26,250 You can create objects, but 58 00:03:26,250 --> 00:03:30,040 if no table exists to hold them, you won't be able to save them. 59 00:03:30,040 --> 00:03:34,230 If you try to set an object attribute but no matching column exists in the database, 60 00:03:34,230 --> 00:03:35,680 you'll get errors. 61 00:03:35,680 --> 00:03:39,330 You always want your code and your database to be in sync. 62 00:03:39,330 --> 00:03:43,020 For this reason, Rails keeps track of which migrations you've run and 63 00:03:43,020 --> 00:03:44,420 which you haven't. 64 00:03:44,420 --> 00:03:47,710 When it saw the create owners file in your migration directory and 65 00:03:47,710 --> 00:03:50,589 saw that it hadn't been run yet, it raised an error. 66 00:03:50,589 --> 00:03:54,080 Run the migration and your database and code will be back in sync and 67 00:03:54,080 --> 00:03:55,200 that error goes away.