Updating the Model3:47 with Jay McGavren
Our app is able to store titles for our posts, but we forgot to add a post body to hold the actual post content. In this stage, we're going to fix that.
Any time we want to update a Rails database, we need to use a migration. And to create that, we'll need the "rails generate" command:
rails generate migration AddBodyToPosts body:text
- The migration to create the "posts" table was set up as part of the scaffold we used earlier. This time, we'll need to create the migration separately. So instead of the "scaffold" generator, we're going to run the "migration" generator.
- Next is the migration name. It should be in CamelCase (with each word capitalized), since it will be used as a class name.
- We can name the migration whatever we want, but if it's in the form
Add<attribute name>To<table name>, the generator will set up a command to add a column to that table for us. So we name it "AddBodyToPosts".
- Next we can specify one or more columns we want to add, just like we did when generating the scaffold before.
- Here we specify "body:text" to create a "body" attribute of type "text".
- Previously, for the title, we used a type of "string". But the post body needs to be longer, so we use a type of "text" here, so that the resulting database column can hold more text.
Don't forget, generating the migration doesn't actually update the database. To do that, run
Frequently Asked Questions
Q: In the console, I loaded a
Post.first, but when I type
post.body = "some value" I get
undefined local variable or method 'post'! What's going on?
A: Calling the
Post.first method doesn't automatically assign it to a variable; the
post variable doesn't exist until you assign a value to it. Be sure to assign the return value of
Post.first to a variable with
post = Post.first.
[MUSIC] 0:00 Our app is able to store titles for our posts, but 0:05 we forgot to add a post body to hold the actual post content. 0:08 In this stage we're gonna fix that. 0:11 First we need to add a column to the database, so 0:13 that our post model objects can store the value of their body attributes. 0:16 Then we'll need to update our views to let us show those values. 0:20 And finally, we'll need to update the controller to allow 0:23 new body text to be submitted from HTML forms. 0:26 Previously, we used scaffolding to generate the new resource, but 0:29 scaffolds won't help us modify an existing resource. 0:33 To do that we'll need to get into the real meat of how Rails works. 0:36 Don't worry, we'll take it slow. 0:39 Along the way, you'll learn skills that will give you much better control 0:41 over the way your resources are presented. 0:44 The first problem is that although we now have a database table posts. 0:47 It only has a column for the title attribute. 0:50 We need to add a body column to store the new data. 0:53 Any time we want to update a Rails database, we need to use a migration. 0:57 And to create that, we'll need the Rails generate command. 1:01 The migration to create the post table was set up as part of the scaffold we 1:07 used earlier. 1:11 This time we'll need to create the migration separately. 1:12 So instead of the scaffold generator, 1:15 we're going to run the migration generator. 1:17 Next is the migration name. 1:23 It should be in camel case with each word capitalized, 1:25 since it'll be used as a class name. 1:28 We can name the migration whatever we want, but 1:30 we're going to put ours in the form add attribute name to table name. 1:32 That way the generator will set up a command to add a column to that table 1:38 for us. 1:41 So we'll name it AddBodyToPosts. 1:42 Next, we can specify one or 1:48 more columns we want to add, just like we did when generating the scaffold before. 1:50 Here we specify body of type text to create 1:54 a body at we have to type text, previously for the title we use a type of string. 1:59 But the post body needs to be longer, so we use a type of text here so 2:05 that the resulting database column can hold more text. 2:09 When we press Enter, the generator creates another file within the db/migrate 2:12 subdirectory to hold the migration. 2:17 This basically says that we should add a column to the post table with the name of 2:22 body and a type of text. 2:27 Don't forget, generating the migration doesn't actually update the database. 2:30 To do that, run rails db:migrate and hit Enter. 2:34 Now that we've run the migration, the body column has been added to the post table. 2:43 And all our post objects have a body attribute available. 2:47 We can see it if we run bin/rails console. 2:50 And load a post object and there at the end is our body attribute. 2:57 The value of the attribute is no by default but 3:04 we can add a new value from the console. 3:06 So we can say post.body = "This is my first post!" 3:08 Once again this update only exists in your system memory. 3:19 It won't be saved to the database until we save the post object. 3:22 So we call post.save. 3:25 We see an SQL query updating that record. 3:29 And if we access the database again. 3:35 For example, by calling Post.all, we'll see the updated post body in the results. 3:39
You need to sign up for Treehouse in order to download course files.Sign up