ActiveRecord: Part 16:42 with Jim Hoskins
In this video, we begin our look at the methods provided by the class ActiveRecord::Base, the default superclass of all models. We start by looking at the methods that allow us to retrieve records from the database Download the completed code.
[?mellow guitar music?] 0:00 [Think Vitamin Membership - Est. 2010] [membership.thinkvitamin.com] 0:03 Ruby on Rails: Basic Associations: Part 1 with Jim Hoskins] 0:07 Often, when creating web applications, we need our different models 0:12 to be associated with each other in different ways. 0:15 In this video, we'll see how ActiveRecord makes managing those associations easy. 0:18 So here in TextMate, we have our Rails project that we've been working on 0:23 open and ready for us to edit, and if you haven't been following along, 0:26 you can download the code from this video's webpage 0:30 and all you need to do is unzip it into a folder like this. 0:33 I'm assuming you have Ruby installed. 0:37 What you'll want to do is make sure you have Bundler installed 0:39 so something like $gem install bundler 0:42 and you may have to go ahead and do a $sudo for that. 0:46 And once you have Bundler installed and you're inside of the application folder, 0:52 all you need to do to get it running is type in $bundle install 0:57 and this will install Rails and all the other dependencies 1:01 in order to get this application up and running. 1:04 Now, to start up this application, all we need to do is type in $rails s or $rails server 1:07 and it should look something like this, which means we can now go over to our browser 1:15 and go to localhost port 3000 and we should see this application. 1:19 Now, since the database was bundled in, 1:24 you should have this same data that I'm looking at here. 1:27 So far, we've implemented projects for our task manager 1:30 and ultimately what we want to do is for each project 1:33 to be able to implement several tasks within that. 1:36 Now, what we've implemented in this project so far 1:40 is projects, so we can split up our tasks into different projects. 1:43 Now, the next thing we want to do is actually create tasks within those projects. 1:47 Let's think about what this means for when we build our models. 1:51 Every project in our database will have zero or more tasks associated with that project 1:55 and each task in our database will have exactly one parent project associated with it. 2:00 Now, to represent this in a relational database, 2:06 what we can do is assign a project number 2:08 to each of our task instances. 2:11 For instance, if we had a task and we saw its project number is 3, 2:14 we can locate that project instance by searching for the project with the id of 3 2:17 in the database. 2:22 Conversely, if we have a project--let's say it's project number 4-- 2:24 and we wanted to know all of the tasks that belong to that project, 2:27 we could search through all of our tasks and look for the ones with project number 4 2:31 associated with it. 2:36 In real terms, what we'll do is call this property on our task the project_id 2:37 and its value will correspond to the id value of a certain project. 2:42 Now, this project id property is what we call a foreign key, 2:48 which means it holds the primary key or id value of some foreign model. 2:52 Now, if were coding the database queries ourself, 2:57 we could create different queries using the joint operator 3:00 to efficiently pull the associated objects from the database, 3:03 but in ActiveRecord in Rails, it's much easier. 3:06 So first, let's actually create our model. 3:10 To do this, I'm going to go over to our Terminal 3:12 and I have a new tab open that's in our projects directory here 3:14 and we'll go ahead and use the Rails generator to create it. 3:18 And we're going to use a scaffold generator 3:22 because eventually, we will want the views and controllers for our tasks, 3:25 even though right now, we're just going to be primarily concerned with the model. 3:28 So what we'll do is $rails generate scaffold 3:32 for the Task model. 3:39 Now we need to define the properties that we want associated with our Task model. 3:43 The order we define these doesn't matter, so I'm going to start with the name. 3:47 I want each Task to have a name, and that's going to be a string, 3:52 so we'll do name:string. 3:56 Now you can see it's sort of broken onto two lines, just to show you 3:59 that it's all one token there, 4:01 I'll just add some spaces, and the next property we want to do is completed 4:04 because we want to be able to complete our task, 4:09 and this will be of type boolean, 4:12 And now we need to add our foreign key that we were talking about, 4:16 and we're going to call this project_id: and we're going to give it the type of integer. 4:19 Now, the reason we're calling it project_id is because it's a convention 4:27 when we're defining foreign keys for it to take the form of the name of the model _id. 4:32 In this case, this foreign key relates to the project, so we'll call it project_id. 4:39 Now it's of type integer because it will match up with the primary key 4:44 or id column from our project, 4:48 and we give it the type integer because it matches up with the id property 4:52 of the actual project class. 4:57 So let's go ahead and create this, 4:59 and it looks like we've got everything we need 5:04 so we're going to go back to our code and we'll open up the CreateTasks migration file. 5:07 You can just open it up by searching for it, or you can go to db migrate 5:13 and look for the migration that ends with CreateTasks. 5:18 So we can see we have the string :name, 5:22 the boolean :completed, 5:24 the integer :project_id 5:26 and it will add the magical timestamps for created at and updated at. 5:28 So that looks good to us, 5:32 and let's go ahead and run that migration. 5:35 So in order to do that, we do $rake db:migrate 5:38 and we've created our tasks. 5:45 Now let's open up our Rails console to see how all of this works. 5:48 So I'm going to type in $rails console or we can just do rails c 5:52 and so we can mess around and we can see Project.all. 6:00 Cool. 6:05 We can see Task.all and we can see that it's empty right now. 6:06 And now we've defined our new model with a foreign key. 6:11 In the next video, we'll see how to tell ActiveRecord to use this foreign key. 6:14 [?mellow guitar music?] 6:18 [Think Vitamin Membership - Est. 2010] [membership.thinkvitamin.com] 6:19
You need to sign up for Treehouse in order to download course files.Sign up