Ruby on Rails Table Association

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
going to go through the assignments that i wrote out in this bootcamp assignments uh bootcamp assignments list let's see how bad i mean that lag is pretty bad look at that because i'm doing the google hangouts and i'm doing a screen share at the same time or a screen recording but just have to live with it cool so create rails application crud can use scaffolding at first so if you have rails installed rails new uh first assignment you should be able to just run rail's new first assignment then run scaffolding after that then that's all you have to do and then push it to github let's see here so it's doing the webpacker install tick tock anyway add a model migration for author um create a second table add a foreign key to books has many belongs to relationship i'll just do these two together um rails g scaffold um scaffolding creates crud automatically including all views controller methods and routes within the application uh and the table so books title string and then a published year entity and then i'll use scaffolding for author and then i'll kind of combine the two rails g scaffold author name string um age is more natural than birth year right birth year sounds kind of weird birth year integer okay so for the beginner you can see scaffolding is creating all of the views for um list update new form the form is a it's a partial that's shared between all the other views it creates the database migration it creates some tests even um i don't think i'll go into that right now and then it creates the controller and so i don't know why this next scaffolding command is not going maybe it should be authors it's kind of difficult to know what should be singular and what should be plural and uh remember the the controller is plural it's books and then the model should be singular um let's see here where's the model yeah there we go so we just exit this process gracefully and then this command should be able to run there oh it's an invalid command that's why there we go see that colon right there cool so database migrations creates the authors table creates the books table so now i will add another migration to add lg add author add author id to books so now we're going to ask mr google what to do here i i bet i could i bet i could guess what the syntax is without looking at google oh and you know this is definitely the nerves of presenting my work i never would have made such a done-done error as that one so i think it's just add column books so it's add column then the table then the name of the column and then so it's like this add column books author id integer i wonder why this one isn't working because this one should definitely work rails g osg migration add author id to books so like some yeah i mean this is just that's just um nonsense it's just deprecation warnings and i don't know why i have the deprecation warnings it sure doesn't make me look very professional but yeah so now we need to run all of these migrations we may need to create the database first okay i guess the database was there already um yeah and we've got author id on books and everything oh it's an sqlite database not cool oh well um yeah so then in the models we need to define the relationship between uh the two tables but let's go into the application first to create some test data i wonder if anyone joined my google call yet i sure wish they would pretty lonely nope no one's here oh that's life i guess uh so let's do mice and then don't know it's persisted and then we do i'm just guessing here uh does anyone know when john steinbeck was born so now we go into the rails console and then we will show off the model association that we just created so book.last there's the book author.last there's the author so now book.last.updateauthorauthor.last so here author id is nil this is going to set oh actually this isn't going to work because the association hasn't been defined yeah so you can assign variables that way once you've defined the association between the different models but for now we have to just treat it like an integer rather than an association so now we go back to book.last and then we see that we've set author id equals one so now we go into the models and we can define the relationship so author has many books and then book belongs to author let's see here so now when you make model changes you have to restart the console for some reason i think it's because they only get loaded once or something but now we should be able to do book.last.author yeah so this has defined the association and we can do author.last.books so this defines the association between the different tables let's see so the next step would be we have to add we have to add the author to the different views so we go into books then we do show so let's go let's see here so yeah right there we're just going to create another box that has the author and so we do so this queries for the associated record um book.author.name and then we just refresh so see this is coming from a different database table but it was really natural the way that we were able to apply it to the view here let's put it on the list view as well index so we need this is going to produce it's a relatively inefficient query because um it's okay for the trial assignment but so each time we do this it's going to run an additional query like select select name from author where author id equals one so that's not efficient but it doesn't matter for the purposes of this demonstration book dot author dot name let's see so now we go back to the list view yeah okay there so john steinbeck and now we just have to create it to the edit to the form um so options for select rails from model um so a form is basically html a form makes an http request with the various parameters that you put into it there's form helpers in rails that do everything for you so what this form with does is it compiles into the html of a form which will look like this so this is the form that it compiles into it's action equals post method equal or action equals books method equals post uh the authenticity token ensures that it kind of protects your controller methods um and make sure that your controller methods are only accessible from forms that were created by your rails application to protect you from cross-origin requests i think it's called it's not it's not cross-site scripting and then within your form you have the title and then the input types are all nested within an object named book so because because this was created by rail scaffolding it's really strongly associated with a model not all of your forms necessarily have to have such a one-to-one relationship with the model but you know for the purposes of this exercise this one does so we need to create a drop down a select so we need to create a drop down select which is going to query from all of the authors in our author table and that's going to create a drop down with all of the authors which is updated based on the authors that we have in our database so we could do this manually right but rails will 100 percent just do it for us um let's see rails options for select collection from model and so i'm just gonna wildly google until i get whatever the there we go so that's it people is um it's probably the instance variable that we need so we're going to copy paste this change it to author then we're going to put this in um i wonder if it needs to it probably does i don't know if it needs to be form dot or not i'll try it both ways and then so here should be author.all and that's going to pluck those two fields i'm going to pluck the fields id and name and then yeah i mean we give it name for the author right so then we go back to this form and then it might error out yeah it did in fact error out so it didn't know how to do it okay and now it did not okay i think this was not a select helper this just creates the option tags and so i think that was not the right rails magic to be applying here um so yeah it just creates the option tags so we need to do a so use that with a select tag collection select um blah blah blah collection select blah blah blah author. wow it's it's exactly the same it even uses the same database model as the one that i'm using author.all so uh this probably probably should be uh book because book is the model we're applying it to name with initial should probably just be name and i don't know what prompt true does [Music] um oh that's it either gives you a prompt or uh you can define what the prompt should be so like the uh the null please select yeah so there we go there we go john steinbeck and then the next step will be to make sure that we can include this in the parameters on the controller because rails has parameter validation so um name birth year and then is it going to go through his author id or is it going to go through his author um that's going to be author id author id so then what this does is it white lists the the author id i'll undo this and then i'll show you what it looks like without having whitelisted the author id author must exist and then we go over to our terminal unpermitted parameter author id oh no what could it mean what could it possibly mean what could it mean i'm just kidding i was answering my phone um so yeah the parameter is not permitted so that means only allow a list of trusted parameters through this was also created by the scaffolding but this is standard practice in all rails so we allow author id and now let's see what happens still didn't work so if something doesn't work a good rule of thumb is make sure you save all of your files and then you refresh your and you restart your server to make sure that it picked up the change oh i'm not loving this huh wonder i wonder what the problem could be because i white listed it maybe i white listed it on the wrong the wrong controller unpermitted parameter author id let's see so we go into the books controller create and it goes down here and it validates book params yeah i put it in the wrong controller quite unintelligently do it now okay so now we created it with the author john steinbeck and remember when we go to book.last in the database it's not actually the name of the author we're doing a query for the related model using the foreign key of author id so it's just storing the id this is how databases work um i will just set the root url to books and then i'll just put this on i think it's root to books index so i'll just do root 2 books index and then i'll just put this on github then i'll just call it good um i don't know if me changing the time of this demo like i don't know if that was a bad idea everybody makes mistakes you just gotta get in there gotta give it a 110 you know okay yeah so that's good enough so uh oh why is it okay so now i've pushed it to github and now everyone can enjoy uh the fruits of my labor is anyone here no no one's here cool well thanks for joining me gang uh i will see you as soon as i learn to stop this
Info
Channel: Cody The Coder
Views: 304
Rating: undefined out of 5
Keywords:
Id: gVe4NbfQG94
Channel Id: undefined
Length: 21min 13sec (1273 seconds)
Published: Mon Jan 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.