Webinar: Overview of Data Flow in Rails
Ok awesome. Well let's go ahead and just get started then we'll of course have a few more people join us. And then this will be recorded for anyone coming in late. That misses the first portion
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Ok awesome. Well let's go ahead and just get started then we'll of course have a few more people join us. And then this will be recorded for anyone coming in late. That misses the first portion. So I'm just going to jump right into this. So if I don't I want to make sure that everyone has had opportunity to pull this this repo.

large

If you had any problems feel free to let me or Ben know and we can definitely get you help with that. And then if you just haven't started with the rails applications yet and you're part of that part time course don't worry too much about it. We can definitely get catch you up to base with everything when you kind of get to that point and you can continue to follow this recording at one point or another. But for today's lesson I wanted to go ahead and get the Call.

OK. I'm just going to go ahead and mute everyone right now. And then if you have a. Question or something feel free just to amuse yourself that way we just don't get that feedback anymore. But I wanted to kind of cover the rails data-flow. I had a lot of questions about this come up. And I wanted to make sure that everyone had an opportunity to kind of better understand what happens within the rails data flow so inside this app that I've already gone ahead and created I earlier just a resource generator.

So what that's going to do inside the rails is it will create a couple files it'll create this article's controller. This article model and then it creates the articles view folder as well as updating the routes. Now right here and then there is so it created this for us and then it did a couple other things like it added JavaScript assets as well as a style.

large

But for the data flow what we're going to cover today is mostly just the overall flow and what happens within rail so rails follows MVC. And that's going to be your Model View Controller and that shows how each aspect of your app is communicating with your database and then to the web page. So what we've done here is we've created the resource generator and then we've created. We've named it the article.

large

So this created a model with a class of article and we'll get to kind of a few cool things that we can do with the model fold with in this model file a little later. But primarily what we're going to do first is come in here to the articles controller. So this is going to be kind of the bread and butter of what the application will use for that CRUD functionality. And so a CRUD stands for if anyone is not familiar it's going to be create read update and delete or destroy is what rails will kind of use for the naming convention and then here I've already coded all of the individual methods that are used for the basic Crudd feature so here I'll just start at the top.

We had that before action and I was just saying okay inside of the show. EDIT UPDATE AND DESTROY methods run this. Method. That article in that article is down here and it's the private. Method area. And so it's going to pretty much pass this code into each of these four men. So then what we have to do is we have to tell the application what to make available inside each of us. So. We're coming in here we're saying okay in the new index which we have right here.

Let's see. Okay. So in the view of index make available the instance variable articles. And called articled.all
large. So if you're familiar with Israel's Consul at all which I'll go ahead and quickly jump in here. You can call this exact method right. This exact line of code right here. Articled all. And you can see that it's going to call to the database and return every article that we have within the database and within the application. So by running this code it's making it accessible inside of the index page here.
large

So I'll go ahead and open the server now at the server so we can take a look at what's happening. So here what we have now created we've now created it as we say it. We said okay articles controller make available the instance variable articles and it is equal to all the articles in the database. And then inside the index I've gone ahead and I've created articles. each do. So that's going to take every article one by one at a time and then display them separately into these methods.
large

Or in total display them as Article title the article author the article rating and the article body as we can see here. So this is the index view we're using the instance variable of articles and because it's plural and we set it to the articles at all. It's making every one of those articles available. And then next we'll go into the show. Or the individual postgres. So that's going to be available here and the controller will call this. with the show method. So we're saying okay now controller in our show page which we have right here.

Go ahead and make available the individual post, and that is getting it from this source before action. That's then setting the article for the article that fine. And it's finding it by the individual post parameter. So I'm going to jump back into the cons. again so you can kind of see what's happening here.

So here at the bottom how close out so everyone can see it a little better. But as we open it we can then again type in article does fine and we'll just start with this specific idea of one name. So in the end this is going to be what it's pulling for each individual show page. And so there we have our first article with Idea 1. So now in the views you can add the are up here you'll see the local host 3000 articles and the id of one. And so that's making it accessible through this article's controller.

And now you can see here on the show page. We're again breaking it out individually to the article title the article author rating and body. And this time we're using the. Instance variable of article which is being made available by the said article. So the next one that you're going to generally you'll be using is trading updating and deleting so we'll go ahead and kind of walk through the same process we've been going through. Last couple of times we'll go to create now.

And what now becoming available to us is it's going to give us this forum which we're using right here. As a partial. And you can call a partial simply by saying render the partial and you know. So you'll name the part you'll always name the partial with an underscore and then. Continue with the name and the regular syntax of. You know about Jarobi. And then you can call the partial just like I'm doing here with render the name of the partial without the underscore and in this case we're actually going to pass in the variable article so that the. Form knows that it's accessible so we're saying article.

And then pass in this instance variable which then allows it to communicate correctly with a model of article. And then it's giving us each individual text-field. So we can just say here we'll say Bottega. Web and our Author or authors James and we'll just give this a rating of 5 right now and then some text. So as we create that it is then coming back to the controller and the new page first required that we called Article Doc Neal and then it responded to this create method which then continued to go through and run the information because our ARTICLE WAS CREATED successfully and then we can also go through and edit it.

And edit then once again you can see up here at the top it's calling in that ID. So it's coming down because it's available in this before action. The edit page it's knowing that it can find the parameter ID and then add it and edit was also. Calling in. This form partial. So we did the same thing here. Now this refactor tebe create this partial just makes it clean because this is preventing us from having to have the new and added page have a lot of just duplicate code here.

So that's a basic overall view of what's happening within a controller right now. And then of course we do have the delete which is referring going to be very similar. It's calling this individual ID. It will delete it and then redirect us back to the home page or the index of our. So do we have any questions at this point. And we don't have any question that can now kind of jump into it.

So now that we understand what's happening. Sorry could you just leave. Is there a way to do recap of. Like it's. A short. Scaled down version explanation of what this is! This is part of that you're expanding the controllers right now. Yes so I'm just going through kind of what the controller was communicating with the view pages and he made It so so now as I talked about where I'm getting a really bad feedback right now.

OK. So what we're so now what we're going to do is go over the whole model of controller and how they communicate with each other so the model here is how we can eventually set individual rules within the model or the generator of article. So this is where you can verify or validate parameters so if we want to make sure that our article always has a title author body and so forth we can put that here. This also is a place where we can go and "create" a custom scope with it.

Creating that scope and I will cover that towards the end of this webinar. But it's allowing that scope to be available throughout the application. So the model I like to think of it is kind of setting the rules for how the. Specific article or whatever you're using can communicate with a Web site or the web application and the database and it's a really good way of thinking as kind of like the guidelines the laws within our society. So we will just set that if it's empty.

A lot of things will still work but you may have errors. You're susceptible to a few more hacks and so forth. And so we can go over that a little bit more but so what the controller will always do. It'll come in and it will communicate with the article to make sure that it's following all the rules and the guidelines and then as it comes in it'll go through the database using these calls such as this.

And if you are ever kind of curious of what is what each individual thing that you're typing in here is doing then it's super fun and useful to come into the console and kind of type in and duplicate what is here what is available here and how that's responding to the database. So once the controller. Sets the instance variable it will then call the database for whatever. You're asking for. So in this case it's all the articles or in this case it's saying "OK let's create a new instance of the class" which it's getting from right here the class of article.

So the new instance of this class is allowing us to create a new one. So as a controller communicates with the database we now have to go into the use and allow the views to. Have access to the specific variables so that's kind of what I covered really briefly before. As. You see women so we're right here in the Index of articles I'll blow it up a little bit. Just so everyone can see and I put little labels here this is the index where we are using the instance variable of articles.

So that means because we made articles available here in the controller for index that the index now has available this specific. Instance variable it can then call. This information right here and make it available inside of that view page. So that's the overall data flow it's going from it's checking with the model for any rules any guidelines that or any validations that need to be made and then a controller is communicating with the database and then to the view page and their view page is pulling all its information from the controller since it has called up from a database and that's what's making all of this information available as far as creating using each individual post and doing it as a whole.

So the next thing I want to kind of go over is just how you can create a custom scope and this is kind of just so you can see some of the rules or features of what you can do with the model. So I'll go ahead and close these. And then what we'll do first. Closed the server really quick. So what I want to do first is I'm going to run a new resource generator or a new generator inside rails. But this time it's going to be just the controller.

It's called rails g and do controller and then type pages and we're just going to do home about favorites. So when you're generating or running a new generator inside rails it's always important that everything's lowercase except for the name of the generator itself. So in this case it's pages and you'll go ahead and with this. So right here I'll kind of explain what's happening first so this is going to generate just a controller and then you use a new page for home about and favorites.

So we'll go ahead and hit enter. And we can see it here. Now everything in green is something new that has been added specifically right here. We're going to have our Controller of pages and it's automatically created this these new methods for each of the about and favorites and then it's down here in the views. It's also created those pages. Those pages. So a generator is something that you can do by hand. So you can come in we can first create the controller.

Give it each method and then. Create a new folder and the pages and then you'd also go onto the routes and make sure that you set goals but a generator just something really nice that Rails is added to make everything quick and easy for it. So with this generator now what I want to get into is creating a custom scope. So we will come into our market our model of article and what I want to do is here in the favorites page. I want to be able to display all the articles that have a rating of four or five.

So what I'll generally do when I want something specific like this is I'll come into the rails console and I'll start looking at something. So first we know articles all and what we could do. Article dot first dot. Rading and these are all things that. Rails has made available so Rails has made available this D'Arte rating. So when we call a specific post we can see its rating.

We could also do Redstart first dot title to see the title of our first post. So what else are doing is to find the specific Scope that I'm looking for is I'll just start playing around with it using various different methods that you can call. In this instance I'm going to use the article dot where and we could do something like this.

First Heidel will look for. The wing fighter. So it looks like we have. Just one article with that title. So that's kind of how the console is communicating for the specific one that I'm looking for it's this one that's really neat. We're going to call in and we're going to say okay looked at their rating and then checked if it's greater than. And you put this. Query into quotations. And then you just put a comma and you put the specific number that you want it to be greater than.

So in this case we're saying everything above 3. And because my search was successful or we know that this search was successful because it pulled everything and if we kind of glanced in here a blue make it a little bigger. You can see all my ratings are for. Five. Five and so it looks like it was successful with what we ran.

So what I can now do. Is come in here and I'll copy the wear and the remainder of what I had called. So where on. And then I'll come in here and I'll say OK. So let's now create a new a new search and this is INSIDE the model. So again it's kind of creating a rule and a guideline of what the application can and can't do.

Now what we'll do is when you're creating a custom scope and a method like this you'll always type and sell first and then the name of the method. So in this case I'll just call up my favorites. The Lecia are actually the top rated. When this really could be this top rated could be named whatever you want. Now why you need this self is important to kind of understand it.

What happens is you can see that this self is actually going to just kind of come in as a placeholder for article. So once we have this saved. Owk go ahead and close out the console again and then come back in.

And now if I type in article dought cooperated. Quinsy cooperated. I get the same query as I did before. So now with this custom scope we've told the model that we can now call top rated and make it available anywhere within the application that is calling on articles. So now what I want to go ahead Endale is come into the pages controller.

And I'm going to go ahead and ask for or I'm going to create a variable and then ask for this custom SCO so that my custom scope can be available inside of this favorite's page. So I would call this variable. My underscore favorite's. And then all you have to do is do article dot. Top rated.

So now just as we saw in the articles controller when we wanted inside the index all the articles to be created we created an instance variable. And then we called in the article also. All would be available. So and here I I'm not done anything different. I've given it a variable that can really be named anything you want and then I've just called article and my custom sgo. So now what I'll do is I'll come into favorite's and I'll go ahead and just clear out all this information and we'll go ahead.

And we'll now ask for rails to put each of these articles into something readable just like we have here right now an index. So I'll go ahead and I'll call that same variable that I created inside of my controller. And I'll call the each method and then do. And now I'll pass in some different variable for it to be available within site or do. Next thing to do kind of just for good practice because we're using embedded Ruby and calling a method always next just come in here and make sure that I add my end in so that I don't for getting run into any errors.

So with this created it's now going to allow me to break each of these articles into their. Individual titles ratings authors and so forth. So what I'll do is I go ahead and just call embedded Hruby and because I want this display to out the equal sign to this particular method and then making it available through that do loop I just have to cull favorite. Let's all call favorite title. And I'm going to go ahead and actually put this inside. Of h1 tag.

And then I'll go ahead and create a paragraph and I'm going to ultimately do this same thing as I did before so I'll call him better Grooby and I'll say favorite dot author no clothes at all. What a bar. I mean I'm just going to type underrating and then I'll call that and better Grooby again. And then last of all will go ahead and will create one more paragraph. Tag with the embedded Ruby. And pass in our favorite body. So with this now we should be able to individually see each post when we're going to their favorites page so I will go ahead. That's been saved. We'll make sure that we start the server again.

And go the pages for it slash favorites and now you can see that each article that has a rating of 4 or 5 is now available to be individually broken down to into this individual sgo. So what you saw here was again just using that whole model of controller. So we set the rule inside of article making top rated available and then inside of the page page controller are actually calling the article model with this new method or custom scope and setting it as a custom or in a custom variable instance variable of my favorites. And inside of a favorite's page we're calling that and running and do each loop for the favorite add posts are there posts that are ranked rated for.

We have any questions. Up to this point I have a question. Yeah. Can you do you have to put that custom scope in the model can you put that. Where is greater than rading 3. Could you put that in the pages control or in the in that method. Your favorites method in this case you cannot just because the the more. So what's what's allowing this to be created is just a way that it's communicating with the class or with a model of the article and you're setting the top rated to be available so if you were to put it just if now.

Well okay let me actually take that back. So technically if you want to I believe you could do. Where and we could type in that same. Yeah. Yes you could do. Yeah you. So you can make that available and Eilish. Are you ready. Let's see three. So now. Yes this will be available.

But this is just a lot of long syntax so if you're trying to call it anywhere else for example it would let's say we didn't have it here. So I'll close that out. And I'll come back into my terminal come there. I'll come back into my consul. And. So about again an article about top rated you can see right here is no longer available.

So if I were didn't want this custom scope anywhere else it's no longer available because I only have it set right here so you can you can create yes you can look at the search rate here and manually type it but it just becomes a lot of bulky heavy code. Where is this adding it here in the model.

Create something nice and clean. Ingens can I jump in real quick. Yeah absolutely. Yeah so in rails we have a saying Andrew says the salon where we want fat models and skinny controllers so in theory yeah you could do that logic inside of your controller. So you could call article where whatever.

But then you have a lot of messy ODing your controller and so just bad practice. Yeah you could do it but it's it's it's a better practice. If you create a custom scope and you do all of that logic inside of your model. Gotcha. Yeah. Thanks Victor. So yeah that's exactly exactly right because especially I mean this is a really small application with just one model set up and not a whole lot of call ins or anything but as you get into enterprise level applications. If you had every custom scope that you were looking for looking like this. Just over and over again it would look a lot messier where in this case.

The top rated most people could come in here and be like OK it's the top rated posts or articles and so it just creates a really clean code that ends up kind of reading and very clear very perfectly well something to keep in mind is if you keep reusing those methods over and over. Thinking.

If you just wanted the end of that one method how many times you don't have to do the unless you know whereas if you're in the model you don't have to change it once we recall it over and over. Dallas. Perfect. And I went through that at one point when I was creating this custom scope and it was.

I mean it was a pretty long line of this query that I had put inside my rails console and before I put it into a custom scope. I had it in about seven or eight different places so if I needed a change anything I'd have to go to all those seven different places within my application whether it was in my free method my update method.

It was in my article proudness or maybe it was in my article but it was in so many different places that when I eventually refactored it to just the article if I ever needed to update that it was just instant across my entire do we have any other questions.

So we did get through that a lot quicker than I had planned. So if anyone does have any general questions whether its about rollerskate or anything else feel free to ask them we have about 15 20 more minutes that we can go ahead and cover the. I have another one. So eventually in the model that's where you have all your kind of your rules before you put that custom scope in it was blank. So is there like a set or a list of generic rules that that comes standard and. And what are those and how can we know them. Let's see.

I don't know if there's any that comes standard to any of the other teams that are on right now. No. Or something that just is automatically built into the back end about. Not that I know of no. And last year explicitly trying to do something so the model. That is where we can kind of filter what we allow into the database.

So. James was talking about this earlier where if you're creating a new instance of sorry I don't know what what terms you use if you're creating a new article. For example you have a heading or a title body or rating whatever you can add validations inserted or model to make sure that those specific columns are filled out.

Before you submit it. So someone just doesn't get into your application and spam. You and just create like a million instances of blank articles right. Absolutely. So as he was typing that I kind of I could tell that's exactly where he's going. It was going so I actually added This validates presence of.

So this is requiring a title author and a body. So if we go back to. This I don't know if my servers. It's not here. So if we come into my server and go to articles Nirim talking about the events and if I don't know. If I don't put in my title. It will actually kick an error out now.

I actually did not post so I was just quickly going through this app so I didn't post to where it's kicking out the article. But in normal circumstances it would go ahead and it would say hey this field must be awful. So as we enter this ill successfully elastic create it and that was made possible just by this validates presence of. And there's a lot of really neat things that you can do. If you're ever kind of curious I would just start by googling it. Or example there is.

One like we could do this that we could say OK take off body. And. We could just invalidates the body presents. It's true. And then laying. Minimum of will say 20. And a maximum of twelve hundred and I mean these are just random numbers that I came up but this is a character count. So now what this is saying is it's also let's see I need to make sure prisons right. But it's also validating the presence of the body just like we did here.

But now it's saying OK require a length of at least 20 characters. So now if I come back and we come to create a new one again. It's just loading. But if I put in less than 20 characters it also won't accept until I have. Those 20 characters and now created it. So that shows something cool that you can use in case of wanting to validate.I mean there's other ways they can also validate like an e-mail make sure that it has the sign and then some sort of text so there is a lot of really neat things that you can do.

But again it is just setting all those rules and guidelines for how the application will respond what the database what's allowed and what's going to be available to the rest application. So you the question before was if there are any like set rules or common things that we normally follow the most something that you're probably going to do every time you build an application is add some sort of validation and you typically do validations inside of the model.

So it just kind of have it in your head that that's like a standard thing that you do just to make sure that your application is going to be built properly. Right. But as you continue building applications and as you start getting into your own habits you'll just know what you have to do inside of the model and you're just start doing that automatically. It's just something that comes naturally with time. And then anyone else have any questions.

I saw you you built this with a resources generator. Yes they built a system like control or is there like a rule of thumb like I know some. A. Lot I know some Ruby developers that want to build everything by scratch and they don't like scaffolding and they don't like resources. Do you find GA's as you're building your own projects that you use particular generators or or do more eyes you know on your own. What's the rule of thumb there.

Yeah that's for me that's definitely going to come down to a thing a preference I've come to really like the resource generators right now is because it doesn't create too much pork but it creates exactly what I need. And then I can continue to have that ability to build out my controller exactly how I want it to. I do personally shy away from scaffolds.

I probably used one or two in my entire time at Bodega. And that was just when I was first starting out just because I don't like how bulky it is and I don't like how kind of messy it gets compared to when your handwriting doesn't have the Jaison code and stuff like that.

But I do know that being said I just like you said I know a lot of coders who will just build everything out by hand so they won't even use a generator. Yeah. So Jordan has a deep dive video where he goes into depth about different generators inside of Brills. He says himself that the most common generator that he uses is a resource generator just because it acts just like a scaffold.

But without that extra code that you don't want right and also use a generator every chance that I get for me is I have to write less code. Scaffold are usually the ones that people don't want to use unless you're just opening up a cookout for whatever reason.

But yeah use generators all the time just so I know that things are built. Properly and don't make silly mistakes like just making sure that I'm inheriting from the application controller. For example if I'm doing new pages control controller or if I'm creating the model you'll use a model generator as opposed to right clicking and doing a new model and then making sure I inherit from an active router and everything just makes my life a little bit easier.

So there's no problem. We like using generators go. Nuts. Now that's great. Any other questions or comments that anyone wants to make. Hey well we're super excited as and as a team Herut Boateng had to give this a jump start with these webinars every so for now we're going to have him every Tuesday and Thursday feel free to let any of the mentors know or any of us know by e-mail or however if there's anything specific that you would like to see covered in a web at our because we're always open to ideas and the more popular requests that we get.

We'd love to kind of incorporate that in for anyone. But. Thanks again for everyone who came and participated tonight and definitely reach out to any of us if you have any questions or want to kind of continue building out this application or. Just get some little I'm a little more or a better understanding of what's really going on within a day. But you guys have a great night and I'll make sure that everyone has this recording here probably in the next 30 to 40 minutes. Thank you guys. Thank you so much. Bye bye.