Adding Records via Rails Console3:17 with Jay McGavren
We've created a Page model class, but our list of pages is still blank. Part of the problem is that no "Page" records exist now. Let's go to the Rails console and fix that.
We've created a page model class. 0:00 But our list of pages is still blank. 0:02 Part of the problem is that no page records exist now. 0:04 Let's go to the rails console and fix that. 0:07 In your terminal, halt your server if it's running. 0:10 Normally we'd type, bin/rails console here. 0:13 But let me take a moment to show you another shortcut, 0:16 instead of the console subcommand, we can shorten it to just a letter C. 0:19 So bin/rails c. 0:23 This is just like using bin/rails s to run the server. 0:25 Hit return, and your console will launch. 0:30 Calling page.all works but 0:33 we get an empty collection back since no records exist now. 0:35 Let's create some records so we have something to display on our list of pages. 0:38 So we'll create a new variable to hold the page object and 0:43 we'll create a new empty page object to go in it. 0:45 That object won't have any attributes assigned so we'll say, page.title, 0:49 About This Blog, page.body, 0:54 Here I'll share my notes about Rails. 0:59 And the slug attribute if you recall will be used as part of the path in 1:07 the browser's address bar, 1:10 once we get that feature implemented in the future course. 1:12 So we'll give this page a slug of just about. 1:15 Now remember, the object only exists in your computer's memory right now. 1:18 It won't exist in the database until we call page.save. 1:21 Okay, let's do another object. 1:26 Create a new empty one, 1:33 page2.title. 1:37 And will put, My Resume in this one, page2.body. 1:41 [SOUND] My name is Jay McGavren. 1:46 [SOUND] I'm a Ruby Developer. 1:51 Etcetera. 1:59 And we'll give it a slug of resume. 2:01 Again, don't forget to save it and let's do a third one just for good measure. 2:09 Page3 is a new page object. 2:15 This one is going to hold info about my collection of robot toys. 2:20 So page3.title, 2:24 My Robot Toys. 2:29 And we're gonna have entries highlighting, 2:33 Zorgboy's classic antique toy I have 2:40 called Moon Explorer, etcetera. 2:46 Again, don't forget to save, oops, forgot a slug for that one, page3.slug. 2:54 And we'll add a slug for this one of just, robots. 3:01 Again, don't forget to save it. 3:06 And now, we can run Page.all again. 3:09 And it will show all of our saved model records. 3:13
You need to sign up for Treehouse in order to download course files.Sign up