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