Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to Ruby on Rails 7!
You have completed Introduction to Ruby on Rails 7!
Preview
Link each user to their Linktree pages using Active Record associations. You’ll see how belongs_to and has_many relationships keep your data organized and accessible.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, so in this video, we want to add
relations between the user and the tree.
0:00
Now, what does this mean?
0:04
Well, first of all, why are we doing this?
0:06
Well, the reason that we are doing this is
because any other user that is signed in
0:08
can delete my user's tree.
0:13
And I'm going to show you that in action.
0:14
So I'm gonna create a tree add light mode,
and
0:16
then what I'm gonna do is copy this link,
go into Edge.
0:19
And this is a different browser,
because if you use the same browser,
0:22
then it's not gonna work because
you'll already be signed in.
0:26
Go to the URL port 3000, then I'm just
0:30
gonna sign up with a different email,
0:35
then add my password 123456-,
0:40
2456, create account.
0:45
And then what we can do is
actually go to the URL, Enter and
0:48
we should be able to destroy it.
0:53
So let's click Delete my tree and
as you can see,
0:55
a different user just deleted this tree.
0:58
So we need to fix this problem.
1:00
Now, one quick, simple way to do it
is to just go to the app folder,
1:02
then go to Views,
then go to Trees and show.
1:07
And then what you want to do is uncomment
this line, you won't have it there, but
1:11
just write this line.
1:15
If the current user.id is
equal to the app tree.userid,
1:17
then show the delete button because
that means that the user is validated.
1:21
Now, you won't actually have
the tree.userid field, so
1:26
I'm gonna show you how to do that now.
1:29
So you need to go to trees form, and
then what you need to do, I'm just gonna
1:32
scroll up to wherever it is, I'm gonna
find it, user underscore id, there we go.
1:37
And here is the input field
where the user id is.
1:43
What you need to do is add
value current user.id.
1:46
That way this will
automatically be filled and
1:50
then you can match it against tree.userid.
1:53
You can match the tree user ID against
your current ID to see if it works.
1:56
Now, this is one way to do it,
and as you can see,
2:00
if I refresh, I can no longer delete it.
2:03
But if I make another tree and
then add the Instagram and all this,
2:05
and then I press next and then I go
into Edge and I try and delete it,
2:10
it's not gonna work.
2:15
No, it actually gives us an undefined
method id and this is really important.
2:17
The reason it gives us this error
is because we're not signed in.
2:21
So what we need to do is to make
sure we're actually signed in.
2:24
So we're gonna say if
user underscore signed
2:28
underscore in question mark and then end.
2:33
That way we will not get this error.
2:37
As you can see,
I'm on a different account, but
2:41
I cannot actually see the delete or
edit buttons, so that's good.
2:44
Let's start, now,
this video is about adding relations.
2:48
So what we're gonna do is
go to app models tree and
2:51
what we have to do is say
belongs underscore to the user.
2:56
What this means is that this
tree belongs to the user.
3:01
And then what we have to do in the user
model is say has underscore one tree.
3:05
Now what you can also do
is say has many trees.
3:13
But I don't personally like to do that as
I don't think a user should be allowed
3:18
more than one link tree.
3:22
So I'm gonna say has one tree and
this is belongs to the user, so perfect.
3:23
Now that we've done that, we can
access things like current user.tree.
3:31
So what I'm gonna do is exit edge and
3:35
what we're going to do is go to
the homepage of our application.
3:38
Then what we want to do is create
a link to our personal tree, so
3:43
let's do that now, go to Views-Home-index.
3:48
What we're going to do is
say link to embedded Ruby,
3:52
link to viewmytree and
then we're gonna say
3:57
current_user.tree and
I believe that might be enough.
4:01
And we go viewmytree and it does work.
4:07
But what we have to do also
is just say if current_user.
4:10
This is another way of saying if
the user is signed in, okay, so
4:14
perfect, that button works.
4:18
So now that we've added the relationships
between the user and the tree,
4:20
we can actually make sure that
our application is safe and so
4:24
another user cannot delete our tree.
4:27
In the next video, we're going to
add a new page called pricing.
4:29
It's purely going to be for
4:33
stylish reasons of the website
doesn't have any functionality at all.
4:34
But I'm just gonna show you
how to create it just so
4:38
we get some more practice
with creating pages.
4:40
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up