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. 0:00 We can create new records in the database by creating new model objects, 0:04 setting their attributes, and then saving the objects. 0:08 We can create a new post object by calling Post.new. 0:12 So let's create a new post, and assign it to a variable, post = Post.new. 0:15 Notice that the Post class name is capitalized whereas the variable name is 0:22 all lowercase. 0:26 You can see the objects we got in the Rails console output. 0:29 Notice that its attributes are all set to nil. 0:32 You can set the attributes the same way you would on any other Ruby object. 0:35 Let's assign a title for this new post. 0:39 We'll say post.title = "Hello, 0:41 console!" And 0:47 right now this object only exists in your system's memory, not in the database. 0:52 If we were to call Post.all again on the Post class, It won't show up. 0:56 To store it in the database, we need to call the save method on the object, 1:01 post.save. 1:05 Notice we're calling save on the objects referenced by the variable, 1:08 not on the Post class with a capital P. 1:11 We'll see an SQL query that inserts a new post in that the posts table with 1:15 the title we assigned, INSERT INTO "posts" with title, created_at, and update values. 1:19 And the value for title is "Hello, console!" just like we entered before. 1:26 If we run post.all again, this time we'll see that our "Hello, 1:31 console!" post gets included in the results. 1:34 Every model object you save to the database gets a value written to its ID 1:39 database column. 1:43 Notice that our post object didn't have an ID when we first created it. 1:44 But now that we've saved our post object, 1:49 if we look at it again, we'll see that it's had an ID assigned. 1:51 As we saw before, 1:56 you can pass an object's id ID the find method to look it back up later. 1:57 So if we type Post class.find and 2:01 an ID of 4, there's our "Hello, console!" post again. 2:05
You need to sign up for Treehouse in order to download course files.Sign up