1 00:00:00,000 --> 00:00:02,000 [?music?] 2 00:00:02,000 --> 00:00:05,000 [Master Class: Designer and Developer Workflow: Adding Job Owners to Views] 3 00:00:05,000 --> 00:00:08,000 [Jim Hoskins] So now we've added the user_id field to the jobs table 4 00:00:08,000 --> 00:00:12,000 and associated jobs to users and users to jobs. 5 00:00:12,000 --> 00:00:16,000 $rails c 6 00:00:16,000 --> 00:00:20,000 And in the last video, we even associated a job with a user-- 7 00:00:20,000 --> 00:00:26,000 for instance, User.first.jobs, 8 00:00:26,000 --> 00:00:30,000 so there is one job in the database that has a user associated with it. 9 00:00:30,000 --> 00:00:32,000 So let's look at what we can do with that. 10 00:00:32,000 --> 00:00:34,000 Let's go into our apps/views folder/job 11 00:00:34,000 --> 00:00:44,000 and the show.html.haml, which is the page that displays when we actually click on a job. 12 00:00:44,000 --> 00:00:48,000 So what we might want to do is below company name, 13 00:00:48,000 --> 00:00:52,000 create another div called .user 14 00:00:52,000 --> 00:01:00,000 and here we'll just put @job.user and let's see what happens there. 15 00:01:00,000 --> 00:01:04,000 So now we see we get a pretty poor representation of our user, 16 00:01:04,000 --> 00:01:08,000 but we can see that there is one associated with it 17 00:01:08,000 --> 00:01:11,000 and that's because the Ice Cream Tester job does have a user associated with it. 18 00:01:11,000 --> 00:01:18,000 If we go to the Fashion Police Officer, nothing shows up in that space, 19 00:01:18,000 --> 00:01:20,000 so what do we want to do? 20 00:01:20,000 --> 00:01:25,000 Well, we could add user.name, which might be what we want to show, 21 00:01:25,000 --> 00:01:30,000 and if we were to go back to the Ice Cream Tester, 22 00:01:30,000 --> 00:01:33,000 we could see that Jim Hoskins is posted here, 23 00:01:33,000 --> 00:01:37,000 but if we go to Fashion Police Officer, we're going to get an error 24 00:01:37,000 --> 00:01:42,000 and that's because the jobs user field is blank or nil, 25 00:01:42,000 --> 00:01:46,000 and when we try to call name on that nil response, 26 00:01:46,000 --> 00:01:49,000 we're going to get a nil method error. 27 00:01:49,000 --> 00:01:51,000 Now, there are a couple of different ways we could handle this. 28 00:01:51,000 --> 00:01:57,000 Now, in our particular case, we're going to end up having it so that no job can exist 29 00:01:57,000 --> 00:02:02,000 without a user associated with it, and that's really a validation issue, 30 00:02:02,000 --> 00:02:04,000 but right now, our data doesn't fit that. 31 00:02:04,000 --> 00:02:07,000 But other situations where you might use this, 32 00:02:07,000 --> 00:02:11,000 having the ability to have a nil value in the association might be what you want, 33 00:02:11,000 --> 00:02:14,000 so one strategy we could use instead of calling name on the user, 34 00:02:14,000 --> 00:02:20,000 we could do something like override the to_s method on user. 35 00:02:20,000 --> 00:02:23,000 Instead of getting an ugly representation like this, 36 00:02:23,000 --> 00:02:26,000 we can actually control what it prints out. 37 00:02:26,000 --> 00:02:32,000 So we'll go to the user.rb, define to_s 38 00:02:32,000 --> 00:02:35,000 and all we need to do is return name, for instance, 39 00:02:35,000 --> 00:02:42,000 and we'll save that out and if we refresh, we can see that it's now Jim Hoskins. 40 00:02:42,000 --> 00:02:46,000 But if we go back to a different one that doesn't have a user associated with it, 41 00:02:46,000 --> 00:02:51,000 since the to_s for nil is nothing, we don't see anything. 42 00:02:51,000 --> 00:02:53,000 Now, that's one solution. 43 00:02:53,000 --> 00:02:57,000 Otherwise, we could do something in our view; for instance, use an if statement 44 00:02:57,000 --> 00:03:01,000 to test whether or not a user is associated with this job. 45 00:03:01,000 --> 00:03:06,000 So if we go into show, what we might want to do here is if there's not a job, 46 00:03:06,000 --> 00:03:09,000 you may not want to show that user div at all, 47 00:03:09,000 --> 00:03:17,000 so let's say if @job.user and we will indent everything beneath it 48 00:03:17,000 --> 00:03:22,000 and this code will only be added to our markup if job.user is true. 49 00:03:22,000 --> 00:03:28,000 So what we could do is, say, add some literal text here: 50 00:03:28,000 --> 00:03:30,000 Posted by 51 00:03:30,000 --> 00:03:34,000 and we can see on this one, it's still empty, but if we go to this one, 52 00:03:34,000 --> 00:03:38,000 we can see now we get Posted by Jim Hoskins. 53 00:03:38,000 --> 00:03:43,000 And the added benefit of doing this if @job.user test 54 00:03:43,000 --> 00:03:48,000 is that we could do something like @job.user.name, 55 00:03:48,000 --> 00:03:54,000 which will return the same thing, but since we're now assured that job.user exists, 56 00:03:54,000 --> 00:04:00,000 we can now safely call name on it and we can easily do things like link_to, 57 00:04:00,000 --> 00:04:03,000 which will link to a specific model. 58 00:04:03,000 --> 00:04:11,000 So we want the link's text to be our name and the link should point to job.user, 59 00:04:11,000 --> 00:04:14,000 and Rails will figure out what the URL for the user is. 60 00:04:14,000 --> 00:04:19,000 So if we click on this, it will go to user/1 if we wanted to link it up like that. 61 00:04:19,000 --> 00:04:25,000 So overall, I think the most effective solution for us is to check if @job.user, 62 00:04:25,000 --> 00:04:28,000 but later on, if we make it so that job.user has to be true, 63 00:04:28,000 --> 00:04:30,000 we can get rid of the if statements 64 00:04:30,000 --> 00:04:35,000 since hopefully, the validations will make sure that job.user is never nil.