Playing Next Lesson In
seconds

Transcript

  1. So let's go about that exact same flow, but for our writers and with some new stuff tied in. So let's go ahead and create a writer's controller

  2. with an index and show method bound in. So we'll stop our server, node, ace, make controller, writers with an index and show method added in.

  3. Go ahead and boot back up our server and jump back into our text editor. For the most part, our query for our index page is going to remain the same. We'll add in some additional stuff to it,

  4. but it will be relatively similar so we can start from it as a base point. So let's do const writers equals and let's paste in what we've copied. Now we'll need to re-import our cinist.

  5. So I'm just going to type out cin and then hit tab to auto import that. And instead of where has movies directed, this will be where has movies written.

  6. Then we can return and we need to extract out our view from our HTTP context, view, render pages,

  7. writers slash index. We can provide in our writers there. Let's get this working before we add some additional stuff to it. So let's jump back to our routes.

  8. We can go ahead and copy both of these, paste it in since we're going to have just writer versions of both of these. Hit option command down to get multiple cursors,

  9. option shift right to select the whole word directors and replace that with writers. Okay, hit escape to go back to a single cursor, jump over to our director's controller here.

  10. Let's go into the middle of it, option command down again to get multiple cursors, option left arrow to jump to the start of the word, option shift right arrow to jump to the end of the word

  11. and select the entire word director's controller. And let's type out writers controller and hit tab to auto import that.

  12. Lastly, command right arrow to jump to the end of the line, option left arrow two times, and then just left arrow once more with option shift left arrow

  13. to highlight our whole director's word, replacing that with writers. If you're not familiar with those keyboard shortcuts, it's worth the time to go through that flow

  14. over and over again so that you get them mentally mapped because they really do save you quite a bit of time. Okay, so we have our routes, let's create our pages next.

  15. We can just right click and copy our director's directory, right click on pages and paste that in. It's gonna be called director's copy, that's okay.

  16. Hit enter on that so that we can rename it to writers. We open that back up, jump into our show page because we have director explicitly written right there.

  17. And let's switch that to writer, switch this to writer. And we're gonna go about this a different approach, so we will need to switch movies, but we'll leave that as is for right now as well.

  18. Our index page has something similar going on. Directors, we'll switch to writers, directors to writers yet again, director to writer, writers, writer, and writer.

  19. Okay, there was more on that page than I remembered, we should just did a find and replace, but that's okay. Lastly, let's jump into our navigation, copy our director's href, paste that in,

  20. switch that to writers and switch this to writers as well. So now we should be able to open back up our browser and we should have a writer's option up here in our navigation that we can click.

  21. And let's see if we had any differences here. A couple, yep. So it seems like Jane Dickey is a director, but not a writer and Bernie Rogan is a writer, but not a director. We are working with a pretty small pool of people,

  22. so there's not gonna be much of a change here, but we do see some change, so we know that's working. So let's jump back into our writer's controller. And now let's say that we want to display

  23. the number of movies that these writers have written. After our where has, we can use a method called with count to count the number of movies related to this user

  24. where they have written the movies. And for that, we can use our movies written relationship. This will add a count onto the extras object of our writers.

  25. Our model query builders will always return back an instance of the model. Any properties that don't exist on that model explicitly included inside of our query,

  26. like our movies written count will be, will then be placed on the extras object. And just like our where has here, we can copy that and add that in as a second argument

  27. to our count to restrict our movies written count to just movies that have already released. So if we dive into our writer's index page,

  28. let's do a span class text slate 400. And we'll display this count inside of parentheses and we'll do double curly braces here

  29. so that we can reach into writer dot and we'll reach for our dollar sign extras object. And there should be a concatenation

  30. of the relationship name underscore count. So that would be movies written underscore count. All right, let's see if we got that right.

  31. Let's jump back into our browser and look at that. So we have Alvina Barrows wrote 45 movies and we need Doyle 42, 42 there, 35, 34.

  32. Awesome, so that seems to be working. Let's hide our browser back away, jump back into our writer's controller. Now let's say we also wanted to do with count, but where the movie is not yet released.

  33. So movies coming soon or something like that. Well, we can give this specific with count a name by inside of the query callback,

  34. chaining off of it and as, and then providing the name in there. So we could do something like released count there.

  35. Let's give that a copy before we save, paste it in, switch our query scope to not released for the second count and switch the name to not released count.

  36. Now we'll give that a save so that it formats, jump back into our writer's index page. And now we need to switch this reference from movies written underscore count,

  37. which is the automatic name that Lucid will provide to it, to our manual name of released count. And we can add in released comma,

  38. and now we can do writer.extras.notreleasedcount as we'll just say not released there. Let's jump back into our browser

  39. and our released looks the same, but we do get back zero across the board for not released. So if we dive back into our text editor here,

  40. go down into our database and our fake cedar. Right here we have two movies being created with written soon and they should have

  41. one specific writer attached to it. So we should have at least two people with a not released movie. Let's take a look here. So right here, we are limiting our writers

  42. to just those that have a released movie. And within our fake cedar, we're creating two specific writers for these not released movies.

  43. So we do wanna get rid of this release specification here because that limits our writers to just those that have written a movie and that movie has released. So if we get rid of our query scope there

  44. to include those that have not yet released, give that a save, jump back into our browser, give this a refresh. There we go.

  45. And now we have one, two, three, and four writers who don't have a released movie, but do have a not released movie. So let's jump back into our fake cedar

  46. to make sure that that is right. So we know that we have two here. Ah, yeah, post-production is also not released. So that would be four. Okay, cool. So that looks right there.

  47. So let's jump back into our writer's controller and let's go ahead and fill out the show method. So again, for this one, we're gonna want view and params.

  48. And now we can get const writer equals await cinist. And this time we're gonna use the cinist query builder to find our writer.

  49. So we can do .where that writer's ID is our params ID. If you wanted to, you could also provide this as an object.

  50. So ID as the column and params.id as the value, that will work as well, which will limit our cinist down to just the one cinist that we're looking for.

  51. And then we can do first or fail. And now we have a query relatively similar to, if we scroll up and go to our director's controller, this line right here.

  52. So both of these lines get us the same result. However, we can use eager loading,

  53. so preload to attach the movies that this writer has written. And since on our index page, we're including those movies where they are not quite released,

  54. we'll leave this as is. But if we wanted to limit this preload to just movies that were released, we could do query, query.apply, scope, scope.released,

  55. like so to get that result. But we'll include those here. Okay, let's go ahead and return,

  56. view, render, pages, writers, show, and provide in our writer. So now we have all of the information on one variable of just writer,

  57. whereas on our director's controller, we had it as director and movies. So within our writer's show page, we're gonna need to reflect that. So instead of just movies here,

  58. this is going to be writer.moviesWritten. And then we can loop over each of the movies that they've written and apply their card. Okay, so now we should be able to dive

  59. back into our browser. I can't quite recall who we were working with on our writer two lessons ago, so we'll just click on somebody random here. And there we go, we see a number of different movies

  60. that this person has written. If we go back, let's click on somebody who only has one. So Bernie Rogan right here, just wrote "Grease," okay. So let's click on somebody

  61. who doesn't have a released movie yet. So Johnny here, "Sugar Shack," okay. So all of that is looking a-okay. [ illegible ]

Listing and Counting a Writer's Movies

In This Lesson

We'll learn how we can perform multiple different relationship counts with the same relationship using the Model Query Builder. We'll count our writer's total number of released and not-released movies.

Created by
@tomgobich
Published

Join the Discussion 0 comments

Create a free account to join in on the discussion
robot comment bubble

Be the first to comment!