Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
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.
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 gonna fix that.
First we need to add a column to the database, so
that our post model objects can store the value of their body attributes.
Then we'll need to update our views to let us show those values.
And finally, we'll need to update the controller to allow
new body text to be submitted from HTML forms.
Previously, we used scaffolding to generate the new resource, but
scaffolds won't help us modify an existing resource.
To do that we'll need to get into the real meat of how Rails works.
Don't worry, we'll take it slow.
Along the way, you'll learn skills that will give you much better control
over the way your resources are presented.
The first problem is that although we now have a database table posts.
It only has a column for the title attribute.
We need to add a body column to store the new data.
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.
The migration to create the post table was set up as part of the scaffold we
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 camel case with each word capitalized,
since it'll be used as a class name.
We can name the migration whatever we want, but
we're going to put ours in the form add attribute name to table name.
That way the generator will set up a command to add a column to that table
So we'll 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 of type text to create
a body at we have to type text, previously for the title we use 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.
When we press Enter, the generator creates another file within the db/migrate
subdirectory to hold the migration.
This basically says that we should add a column to the post table with the name of
body and a type of text.
Don't forget, generating the migration doesn't actually update the database.
To do that, run rails db:migrate and hit Enter.
Now that we've run the migration, the body column has been added to the post table.
And all our post objects have a body attribute available.
We can see it if we run bin/rails console.
And load a post object and there at the end is our body attribute.
The value of the attribute is no by default but
we can add a new value from the console.
So we can say post.body = "This is my first post!"
Once again this update only exists in your system memory.
It won't be saved to the database until we save the post object.
So we call post.save.
We see an SQL query updating that record.
And if we access the database again.
For example, by calling Post.all, we'll see the updated post body in the results.
You need to sign up for Treehouse in order to download course files.Sign up