Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Rails Console: Creating Model Objects2:10 with Jay McGavren
Since reading from the database is easiest, we'll go a little bit out of order in our tour of CRUD operations, and start with "read" operations.
We can create new records in the database by creating new model objects, setting their attributes, and saving them.
- We can create a new
Postobject by calling
Post.new. We can create a new "Post" and assign it to a variable:
post = Post.new
- You can see the object we got in the Rails console output. Notice that its attributes are all set to "nil":
#<Post id: nil, title: nil, created_at: nil, updated_at: nil>
- You can set the attributes the same way you would on any other Ruby object. Let's assign a title for this new Post:
post.title = "Hello, console!"
- To store it in the database, we need to call the "save" method on the object:
- We'll see an SQL query that inserts a new post into the "posts" table, with the "title" we assigned:
INSERT INTO "posts" ("title", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Hello, console!"], ["created_at", 2016-07-13 19:50:08 UTC], ["updated_at", 2016-07-13 19:50:08 UTC]]
Every model object you save to the database gets a value written to its
id database column. Notice that our
Post object didn't have an
id when we first created it.
- If we look at the
Postafter saving it, we'll see it's been assigned one:
#<Post id: 7, title: "Hello, console!", created_at: "2016-07-13 19:50:08", updated_at: "2016-07-13 19:50:08">
- As we saw before, you can pass an object's
findmethod to look it back up later:
Frequently Asked Questions
Q: In the console, I created a
Post.new, but when I type
post.title = "some value" I get
undefined local variable or method 'post'! What's going on?
Post.new 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 new
Post to a variable with
post = Post.new.
Let's continue exploring the Rails console with create operations.
We can create new records in the database by creating new model objects,
setting their attributes, and then saving the objects.
We can create a new post object by calling Post.new.
So let's create a new post, and assign it to a variable, post = Post.new.
Notice that the Post class name is capitalized whereas the variable name is
You can see the objects we got in the Rails console output.
Notice that its attributes are all set to nil.
You can set the attributes the same way you would on any other Ruby object.
Let's assign a title for this new post.
We'll say post.title = "Hello,
right now this object only exists in your system's memory, not in the database.
If we were to call Post.all again on the Post class, It won't show up.
To store it in the database, we need to call the save method on the object,
Notice we're calling save on the objects referenced by the variable,
not on the Post class with a capital P.
We'll see an SQL query that inserts a new post in that the posts table with
the title we assigned, INSERT INTO "posts" with title, created_at, and update values.
And the value for title is "Hello, console!" just like we entered before.
If we run post.all again, this time we'll see that our "Hello,
console!" post gets included in the results.
Every model object you save to the database gets a value written to its ID
Notice that our post object didn't have an ID when we first created it.
But now that we've saved our post object,
if we look at it again, we'll see that it's had an ID assigned.
As we saw before,
you can pass an object's id ID the find method to look it back up later.
So if we type Post class.find and
an ID of 4, there's our "Hello, console!" post again.
You need to sign up for Treehouse in order to download course files.Sign up