Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
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
Post
object by callingPost.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:
post.save
. - 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
Post
after 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
id
to thefind
method to look it back up later:Post.find(7)
Frequently Asked Questions
Q: In the console, I created a Post
with Post.new
, but when I type post.title = "some value"
I get undefined local variable or method 'post'
! What's going on?
A: Calling 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
.
-
0:00
Let's continue exploring the Rails console with create operations.
-
0:04
We can create new records in the database by creating new model objects,
-
0:08
setting their attributes, and then saving the objects.
-
0:12
We can create a new post object by calling Post.new.
-
0:15
So let's create a new post, and assign it to a variable, post = Post.new.
-
0:22
Notice that the Post class name is capitalized whereas the variable name is
-
0:26
all lowercase.
-
0:29
You can see the objects we got in the Rails console output.
-
0:32
Notice that its attributes are all set to nil.
-
0:35
You can set the attributes the same way you would on any other Ruby object.
-
0:39
Let's assign a title for this new post.
-
0:41
We'll say post.title = "Hello,
-
0:47
console!" And
-
0:52
right now this object only exists in your system's memory, not in the database.
-
0:56
If we were to call Post.all again on the Post class, It won't show up.
-
1:01
To store it in the database, we need to call the save method on the object,
-
1:05
post.save.
-
1:08
Notice we're calling save on the objects referenced by the variable,
-
1:11
not on the Post class with a capital P.
-
1:15
We'll see an SQL query that inserts a new post in that the posts table with
-
1:19
the title we assigned, INSERT INTO "posts" with title, created_at, and update values.
-
1:26
And the value for title is "Hello, console!" just like we entered before.
-
1:31
If we run post.all again, this time we'll see that our "Hello,
-
1:34
console!" post gets included in the results.
-
1:39
Every model object you save to the database gets a value written to its ID
-
1:43
database column.
-
1:44
Notice that our post object didn't have an ID when we first created it.
-
1:49
But now that we've saved our post object,
-
1:51
if we look at it again, we'll see that it's had an ID assigned.
-
1:56
As we saw before,
-
1:57
you can pass an object's id ID the find method to look it back up later.
-
2:01
So if we type Post class.find and
-
2:05
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