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
There are many ways to add the foreign key column that's required by our has_many and belongs_to associations, and I want to take a moment to show you a couple more of them.
There are many ways to add the foreign key column that's required by our has_many
and belongs_to
associations, and I want to take a moment to show you a couple more of them.
- If you want, you can use the
references
column type in migrations instead.bin/rails generate migration AddPostToComments post:references
- That will create a migration with a call to the
add_reference
method instead ofadd_column
.add_reference
takes a symbol with a table name, and a symbol with the name of a model to add a foreign key for. It'll create a column whose name begins with that model name, and ends in_id
. And since it's always desirable to have an index on foreign key columns,add_reference
will add an index as well. Soadd_reference :comments, :post
will create apost_id
column in thecomments
table just likeadd_column
did, but it will also add an index on that column automatically. - The
foreign_key: true
argument will set up a foreign key constraint on databases that support it.
class AddPostToComments < ActiveRecord::Migration[5.0]
def change
add_reference :comments, :post, foreign_key: true
end
end
Without a foreign key constraint, we could create a comment with a post_id
field set to 999
, even if there was no record in the post
table with an id
of 999
. With a foreign key constraint, the database would prevent such a record from even being saved. Foreign key constraints help keep bad data from sneaking into your database.
Note that the adapter for the SQLite database that Rails uses by default doesn't support foreign key constraints. Your migration will still work, and it's a good idea to get in the habit of adding the constraints in your migrations. But if you want the database to actually enforce the constraints, you'll need to switch to another database like MySQL or PostgreSQL.
If we know we're going to need an association when we're first creating a model, we can set the necessary columns up then, too.
- Let's re-generate the
Comment
model. We can replace the two migrations with a single migration that creates thecomments
table and adds apost_id
column along with the other columns:rails g model Comment content:text name:string post:references
.- We can allow it to overwrite the existing model class file, as well as another file related to tests.
- Don't forget to run
bin/rails db:migrate
. - We should now be able to create comments associated with a post again.
-
0:00
There are many ways to add the foreign_key column that's required by our has_many and
-
0:04
belongs_to associations.
-
0:06
And I wanna take a moment to show you a couple more of them.
-
0:09
Let's undo our changes to the database by rolling back the latest migration.
-
0:13
We'll run bin/rails.
-
0:16
And we'll run the db:rollback task.
-
0:20
That'll go through the commands in the latest migration and undo them one by one.
-
0:25
Then let's get rid of the migration we created.
-
0:27
We're gonna do that with bin/rails destroy,
-
0:31
which you can consider to be the opposite of the generate command.
-
0:35
We're gonna destroy the migration named AddPostToComments.
-
0:44
That's the latest migration we created.
-
0:47
That'll go through,
-
0:48
find all the files that were created by the generator, and delete them.
-
0:52
Now let's redo our previous migration using the references column type instead.
-
0:56
So we're gonna run bin/rails generate.
-
1:02
We're gonna generate a migration.
-
1:04
And again, it'll be named AddPostToComments.
-
1:09
This time, instead of a type of integer, we're going to give it
-
1:14
a column name of post and a type of references.
-
1:21
That'll create a new migration file.
-
1:27
And the file will have a call to the add_reference method instead of
-
1:31
add_column.
-
1:32
add_reference takes a symbol with a table name and a symbol with the name of
-
1:36
a model to add a foreign_key for, in this case, the post model.
-
1:41
It'll create a column whose name begins with that model name and
-
1:45
ends in underscore ID.
-
1:47
And since it's always desirable to have an index on foreign_key columns,
-
1:50
add_reference will add an index as well.
-
1:53
So add_reference :comments,
-
1:54
:post will create a post_id column in the comments table, just like add_column did.
-
1:59
But it'll also add an index on that column automatically.
-
2:03
The foreign_key: true argument will set up a foreign_key constraint
-
2:07
on databases that support it.
-
2:10
Without a foreign_key constraint, we could create a comment with a post_id field set
-
2:14
to 999, even if there was no record in the post table with an ID of 999.
-
2:21
But with a foreign_key constraint,
-
2:23
the database would prevent such a record from even being saved.
-
2:27
Note that the adapter for the SQL-like database that Rails uses by default
-
2:31
doesn't support foreign_key constraints.
-
2:34
Your migration will still work.
-
2:35
And it's a good idea to get in the habit of adding the constraints in your
-
2:38
migrations.
-
2:40
But if you want the database to actually enforce the constraints,
-
2:43
you'll need to switch to another database like MySQL or PostgreSQL.
-
2:47
See the teacher's notes if you'd like more info on doing so.
-
2:51
Now that we have our new migration, let's try running it.
-
2:54
bin/rails db:migrate.
-
3:01
It'll add a post_id field back to our comments table again.
-
3:04
And if we launch a Rails console, we'll be able to add comments to our post again.
-
3:11
So if we were to take the first post and
-
3:16
create a comment on it with comments.create,
-
3:21
we'll give it content of HI.
-
3:27
And the commenter name is gonna be Jay.
-
3:31
And if we pretty print the list of the first post comments,
-
3:37
we can see the comment's been added.
-
3:42
If we know we're going to need an association when we're first
-
3:45
creating a model, we can set the necessary columns up then, too.
-
3:49
Let's start by getting our database back to the point it was at
-
3:52
before we created the comments model.
-
3:54
Since we've only set up the comments table on our development machine,
-
3:57
we can just undo the migrations that created it.
-
4:00
We can list out the migrations in our db/migrate directory with ls db/migrate.
-
4:08
You might need to use a slightly different command to list the directory contents if
-
4:12
you're on Windows.
-
4:13
We can see that the two most recent migrations are add_post_to_comments and
-
4:18
create_comments.
-
4:20
Let's roll back our migration that adds the post_id field to the comments table.
-
4:24
bin/rails db:rollback.
-
4:30
Then let's roll back the migration that created the comments table.
-
4:33
bin/rails db:rollback again.
-
4:36
Now let's get rid of those last two migrations.
-
4:39
bin/rails destroy,
-
4:44
The migration named AddPostToComments.
-
4:52
And then we'll also destroy the migration named CreateComments.
-
5:00
Now let's regenerate the comment model.
-
5:02
We can replace the two migrations with a single migration that creates the comments
-
5:07
table and adds a post_id column along with the other columns.
-
5:10
So we'll say bin/rails
-
5:14
generate model Comment.
-
5:19
We'll set up a content attribute of type text and
-
5:24
commenter name attribute of type string.
-
5:28
And post:references will set up the post_id field.
-
5:35
If it asks to overwrite any existing files, it should be okay to allow it.
-
5:39
We generated the migration, so don't forget to run bin/rails db:migrate.
-
5:47
And if we launch bin/rails console,
-
5:52
we should be able to create comments associated with post again.
-
5:55
So let's say post = Post.first,
-
6:01
post.comments.create.
-
6:05
And we'll give it content of Hi and a commenter name of Alena.
-
6:15
And if we run Post.first.comments, it should bring up our new comment.
-
6:22
Now you know several ways to setup a has_many association.
-
6:26
In the next few videos, we're going to take a look at some of the other
-
6:28
associations that Active Record supports
You need to sign up for Treehouse in order to download course files.
Sign up