Ruby on Rails: What's The Difference Between Joins & Includes?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey it's Casas Castillo from Ruby guide.com and in this video you're going to learn about rails 2 methods and these 2 methods are very important there are they are includes and joins joins and include these are two very important method that you use with your active record models so in this video you're going to learn what is Method 2 how they work and why they are important so let's do this [Music] okay before really quick before we get started make sure to subscribe to the channel if you haven't yet so you can get more videos like this and we seed my website Ruby guides dot-com okay so the topic of this video like I said is these two methods I have here one is include and another is James first thing that you need to know and notice is that these are in plural so it's includes its with the S at the a nice joins okay why are these methods useful well let me show you some examples let's say that we have books model I here I have book and I have an association relationship to another model which is comment so in the book model we have has many comments now what you want to do is to find out which comments sorry which books have a comment ID of two and these the syntax to do that but if I try this I get an error and heiresses no such column comment ID so what's the problem here well I know that a column exists and you might want to check if you're not sure what's missing here is that we need to add the column into the query so that when we do the word and remember we're using for filtering we can actually use the comment table so to do that you can do in two ways one is to use joins until is to use include and that's where these two methods come in and we're going to see the differences which are very important so by default you can use include and here we have to say comments which is the name of this particular rails Association the has many comments I know this word works because now we have include the comments with the book so we can filter through the comment ID in this case the result is an empty relation because there are no books with a comment ID of to not understand the difference between includes and joins we have to look at the actual sequel output so the sequel dis thing right here I have highlighted this sequel journal familiar with sequel sequel is the language for the database and rails generates this for you that's why activerecord is so helpful because it takes care of generating this language for to talk to the batteries right but if we look here we will notice this thing is sounds left outer join and I will explain what a means but first let's do the same but this time this time I'm going to use joins in of includes and you can see that we get the same result an empty relation empty relation the result in this case is the same but the sequel way is different so I'm going to highlight the difference which is this left outer join and here we have inner join ok so that's the difference and this matters because these have different meanings for the database so once the actual practical difference for you well let's find out by removing the word ok now if I instead of where I use size second count how many books I get as a result you see that I get one okay and if I do this with includes and you have to spell it right I get four so we can see that this is a difference between how many books we get when we use joins and when we use include what's the difference well the difference is that when you see you use joins okay now let's see the actual output when you use joins you only get you only get books or whatever your model is this could be users or articles post anything else in this case we'll get books which have at least one comment so that's why we get a different amount of books when I use joins that includes so what it means is that inner join and this whole you can remember this joint it just joins the matching values so we look through all of the books and why we try to match them well the database we try to match them with comments and we only give you books which have at least one comment right so that's what joins to us now the difference with include is that input we give you all of the books whether they have a comment or not but if they do if they do have a comment then it will be if we will include it so that you can use it as part of your world Klaus your world a method for filtering and for another reason I'm going to show you so this not only for well it also has another very important use when it comes for to performance so if you care about performance and having our first application this also welcome now next is also very important understand so we have you seen the base difference between include which produces this left as we can see here left outer join and what a means is include all of the books whether or not they have a comment but if they have a comment and included that's how you can remember remember it or at least how I like to remain include means include all and join means it use much books with matching comments so Giants is always less less results so now let's see there so now let's see the other part were it's very important to use include and that's when we have imagine we are inside a controller and we're going to do something like books hook or something like that right so we do that and so we get out of the books into an instance variable and then from the view receta we want to print how many comments every book has right so that would look something like this book book dot comments signs and something very interesting happens here I discuss a technical name in rels you see that we have four books right we sell our books out science we have four books and in here when I try to open to print the comments how many comments are for each book we get this so what this means is that we're doing one database query for each book one database query for each book on top of we're in for the list of all of the books right so imagine that instead of having four books you have million books that means that you will be doing a million database queries every time you load this page the Tanners that's not very good for verb for performance right and the name for this is called an N + 1 wilee where does that come from well n is forward many and books or users you have and plus one is this list initial query right so the fix this we need to do book includes comments because we're going to use the comments we are going to preload all of the comments with the books which is done in this square right here and then when I do the same thing as before now we then get the n plus 1 problem now we get a more efficient use of the database because we include all of the comments when we requested the list of whoops so does the perform implications of using include and also show you how to use includes and joints for when you want to use wear when you want to filter by some attribute of the comments like a specific comment ID to that joins and includes in rels I hope you learn something new please click the like button for me so I know that you like this video if you want to learn more watch more videos right now on the channel subscribe to the channel if you haven't yet and visit my website Ruba guides calm groobie guys dot-com thanks a lot for watching I will see you in the next video
Info
Channel: Jesus Castello
Views: 2,779
Rating: undefined out of 5
Keywords: Ruby (Programming Language), Ruby Programming, Ruby on Rails, ActiveRecord, Rails Where Association, Rails Joins, Rails Includes, Rails Relationships, Rails Models, Joins Vs Includes Rails
Id: hfPCQgOQ7jI
Channel Id: undefined
Length: 14min 1sec (841 seconds)
Published: Thu Jul 25 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.