Locked lesson.
About this lesson
What are Associations and why are they so important?
Exercise files
Download this lesson’s related exercise files.
Rails Associations.docx58.9 KB Rails Associations - Solution.docx
59.4 KB
Quick reference
Rails Associations
In this video we'll use Rails Associations to connect our pins database table to our users database table so that we can tell which pins belong to which users.
When to use
Whenever you have more than one table in your database and they need to communicate with each other use Rails Associations.
Instructions
If you have any pins on your website already, delete them before doing any Association work.
Next: We want to associate our pins with our users. Add this code to your app/models/pin.rb file:
belongs_to :user
Next: We want to associate our users with our pins. Add this code to your app/models/user.rb:
has_many :pins
Next: We need to add a user_name field with value "integer" to our pins table:
rails generate migration add_user_id_to_pins user_id:integer:index
Next: Push your new migration to the local and production database as we've done before.
Hints & tips
- Always remember to delete all your pins before setting up Associations!!!
- To learn more about Associations, check out the Rails Guides at guides.rubyonrails.org
- Add a user_id to your pins controller with this command:
- rails generate migration add_user_id_to_pins user_id:integer:index
- 00:05 Okay, so we got the ability to create pins and we have the ability to log in and
- 00:09 log out.
- 00:10 But our app doesn't know one very important thing.
- 00:13 And that is which Pins belong to which users.
- 00:16 So I can create this Pin here, but anybody can come along,
- 00:19 as long as they're logged in, and they can destroy this pin.
- 00:23 Because our system doesn't know that that's my pin and mine alone.
- 00:27 And it doesn't know who the correct user is for each pin, so
- 00:30 that's what we're gonna work on in this video.
- 00:32 Basically, we need to go through two or three different steps in order to make
- 00:35 this work, and that's what we'll be doing in the next two or three videos.
- 00:38 So first off, if you have any pins listed here, go ahead and delete them.
- 00:42 It's pretty important.
- 00:43 We don't want any pins before we do all this stuff.
- 00:45 Let's go back and look at our db schema file.
- 00:48 And this is the snapshot of our current database.
- 00:51 We have this users table, which is our devise stuff.
- 00:54 And we have this pins table, which is our Pin scaffold stuff.
- 00:57 But these two tables can't talk to each other, they don't know that each exists.
- 01:01 There's no interaction between them.
- 01:03 And we need to set up that interaction because we need our app to be able
- 01:06 to know that users have pins and pins belong to users.
- 01:10 So in Rails, that's pretty easy to do, and
- 01:13 it deals with something called Rails associations.
- 01:16 We wanna associate one table with another.
- 01:19 So if you head over to Google and type in Rails associations,
- 01:23 there's a really good Rails guide on this.
- 01:26 Now, we've look at these Rails guides before.
- 01:27 If we scroll down here, so
- 01:29 here are the types of associations we have to choose from in Rails.
- 01:33 And let's think about each of these.
- 01:34 Belongs to.
- 01:35 Something belongs to somebody else.
- 01:38 So a pin belongs to a user, a user has one pin, or a user has many pins.
- 01:45 And that's basically it.
- 01:46 There's a few more here.
- 01:47 Has many through.
- 01:48 Has one through.
- 01:49 Has one that belongs to many.
- 01:50 We're not interested in these.
- 01:51 These are a little bit more advanced.
- 01:53 We just wanna look at these.
- 01:54 So in our case, a user can have more than one pin.
- 01:57 So we don't want this one.
- 01:58 We want the has_many and the belongs_to.
- 02:01 And you can sorta read through this guide if you want.
- 02:03 It shows how these things are connected.
- 02:05 And it's a good read, there's a lot of stuff here.
- 02:07 It's a big page, so I recommend that you take a few minutes to kind of
- 02:11 look through here and learn about this stuff 'cause it's really interesting.
- 02:13 But really, when you get right down to it,
- 02:14 all you need to know are these two things here.
- 02:17 So how do we use these?
- 02:19 Well, let's go back here.
- 02:20 It's actually pretty easy.
- 02:22 We want to look in our app models folder.
- 02:25 And remember, models, that's database stuff.
- 02:27 And you see we have these two files, pin and user.
- 02:30 And the pin.rb file goes with our pins table.
- 02:32 And the user rb file goes with our user's table.
- 02:36 And this allows us to write Ruby code specifically to deal with model stuff.
- 02:41 What we wanna do here is, I open up the pin's one.
- 02:45 So what does a pin do?
- 02:46 A pin belongs to a user, right?
- 02:49 So all we have to do is type in belongs_to,
- 02:53 and then a space and then user.
- 02:55 Go ahead and save that.
- 02:58 Now, let's open up our user.rb file, and you can see it already has some device
- 03:02 stuff in here because the user, that's the devise table.
- 03:05 So a user what?
- 03:07 Has many pins, right?
- 03:09 So we just type has_many_pins.
- 03:14 Go ahead and save that file.
- 03:16 And that's pretty much it.
- 03:17 We're basically associated now.
- 03:19 There's a couple of more things we need to do.
- 03:21 And if we come back to our DB file and look at our schema, take a look at this,
- 03:25 users and pins.
- 03:26 So if I was a user,
- 03:27 how would I let the world know that one of these pins was mine?
- 03:30 If I was telling a friend hey pin number six is mine?
- 03:34 Well, there is nothing on here that shows that pin number six exists.
- 03:38 There's a description.
- 03:39 I could say hey pin with the description hello world is mine, but
- 03:42 that's kinda messy.
- 03:43 It would be much better if we named these with numbers, right?
- 03:46 A unique number.
- 03:47 So if we give each pin a unique number, then a user could say hey,
- 03:51 pin number 47 is mine, right?
- 03:53 So we need to set that up.
- 03:54 So we need to add a field here to our pins table, and let's give it a name user ID.
- 03:59 And to do that is very simple.
- 04:01 We're gonna use a generator, as we have in the past.
- 04:03 And the command is rails generate migration,
- 04:06 'cause we're gonna add a migration.
- 04:07 In the past, when we've done database stuff,
- 04:09 we've either used devise to do it for us, or we've used a scaffold.
- 04:12 But now, we're just gonna create our own migration.
- 04:14 And you remember, we have two migration files already.
- 04:16 And we're gonna call this add_user_id_to_pins.
- 04:22 So we're adding a user ID to the pins table.
- 04:25 And what we're gonna add is a user ID.
- 04:27 And we're gonna call it integer.
- 04:29 Integer is number.
- 04:31 An index that's going to tell it to make this number unique for each pin.
- 04:34 So enter that.
- 04:36 And look, boom, we saw this, it popped in a new migration.
- 04:38 And it's adding a column to pins with user ID, and calling it an integer and
- 04:43 making it unique, making it an index.
- 04:45 So we've created a migration.
- 04:47 The next step is to push our migration.
- 04:49 So we run rake db:migrate, as we have in the past.
- 04:53 And if you haven't before now, be sure to delete all your pins, both here and
- 04:57 on your Heroku site, okay?
- 04:59 And finally, we need to heroku run rake db:migrate.
- 05:02 Push this migration up to Heroku.
- 05:05 And while that's going on, take a look at our schema file.
- 05:08 It already added the user ID, named it an integer, and so we're good to go.
- 05:13 So now, we've associated pins with users.
- 05:16 We've given it a feature, a function, a way to look up different pins by user ID.
- 05:22 And we have one more step to do, and we'll do that in the next video, and
- 05:26 it deals with controller stuff.
- 05:27 And so, that's all for this video.
Lesson notes are only available for subscribers.