Ruby | ActiveRecord: how to build a CLI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay I guess okay so it's recording perfect so today we are going to have a very quick intro to the internet that because next week you are starting your real adventure of the internet welcome to the 21st century and and also because actually your your CLI may be so much better or so much more exciting for you but also frustrating if you use external database so we'll look into that in this lecture right so today will also practice right in a command-line application so I will try to guide you through but how to set it up you know and then we'll talk about exactly the Internet and yeah and we'll practice making requests to an API what exactly what we will be doing is we'll review activerecord project setup we will build an app that basically just gives you a dad joke every time you ask and also if we have time it will save that joke to your favorite jokes which is pretty exciting and this we need to change so when it comes to user stories a user will be able to sign up login to the application will be able to search for jokes this needs to be edited and save the jokes and see all the dogs this two are going to happen only if we have time we'll build welcome message and user login will implement the search and will save their results right this is our domain model so you see that there is a user there is a joke there is a joiner that is either joke so what type of relationship is that I'm just checking whether you're not asleep you can just say yeah husband a husband a yeah has many and has many throw awesome so let's go to the up oops okay so as you see I already now know that downloaded the repo I am where am I okay so I'm going to CD to the electric it but if you want to code along you can just or like if you want to code along later at home you can just use starter kit okay so I'm in the lecture kit and I'm just gonna I'm just gonna open it in a separate in the separate editor just so that I'm not confused at any point with the files that are named the same in different folders cool so now let's open the readme it's always good to have a readme wait how do I split screen in this okay thank you cool so okay so review actually active records project setup so now whenever you see does you have a gem file and also you don't have a gem file I would encourage you to maybe start with that and so when you have a gem file it's always a good practice to write bundle in the very torrent bundle in the world begin can you see well yes so now you can see that all my gems have been installed now let's look into those into those gems let's see what we will be working with so now do you know what gems are actually who can explain what gems are like okay go ahead yeah so Mohammed said other people's code that she can use in your app yes that's that's correct it is I like to think about it like extensions to like some to a game you know like for example you had Sims game and then you get Sims party seems animals you know so I like to think about gems like those you know Adams something that makes your app get superpowers right so one of the one of the superpowers that will have its active records active record and then we'll be working on sqlite3 that's the database we'll use pry of course because how else would we would we go through the buggy require all do you know what that that is this is go ahead everything that's in the in a given folder I will show you in a moment faker actually we won't be using it here but it is a good jam if you want to create a lot of fake data so that you don't spend five years creating users with their unique user names and stuff you can just use faker I encourage looking into that we also look into colorize that's really fun it helps you make your app really colorful and restclient which is the main guest of our dinner tonight right so rest client actually allows us to work with the internet which is pretty exciting ok so we've already done that let's let's look into our process so we created the gem file and run bundle now we should create migrations so we already have migrations spoiler talat alert and I'm just gonna in order to second in order to see like I cannot multitask wait did I just I think I did I think I deleted the whole because we don't have time to do to run everything you know like read the whole migration and stuff I will just copy from the starter kit copy and now cool and we do paste will I be positively surprised yes okay but now this we want to delete so this is not really a beautiful thing to do but I'm doing now don't don't learn from me if you want to totally like in a very clumsy way restart your mic like totally start to your migration from scratch you can just delete the development and schema okay so this is what I did as you see we have them migration files and we have the seeds so before we run migration let's look into that so we have create users and we see that the users will be instantiated with name username do you want to add anything else the less is the better okay in drugs we just have the content calm and then user jokes what should there be so what do you think there should be go ahead so what are you saying and what was gonna happen but the but the table already exists for jokes right so maybe we want to just create associations between a specific joke and a specific user so what would you do like this yes user cool how does that look aha imagine Madsen has a question no I mean I'd like to trial people so yeah so exactly this is good to remember that here you were tying this user Doakes to just one user and one joke right singular what would be what would happen if it was plural well it it would work because a ruby would help you out and actually it maybe tomorrow I will try to show you what what happened but today I will just move on but if you are super curious I encourage you to explore that on your own okay but so we know that so this is creature so we already created the migrations right so let's write run break DB migrate let's see what's gonna happen reported don't know how to build task DB do you know what happened there are space yeah okay so the migrations worked we know it because we see it and we also know it because development files development and scheme Mac just appeared out of nowhere now if you want to check whether what you thought you did it's actually what happened you go to schema and you see whether this is what actually you think your tables have so dogs have content and the content is a string it's not an integer it's a string right user dogs integer integer joke ID user ID and table users has a string username awesome but now let's look into our readme remember never ever ever change anything new schema like that series yes Shammi like this so exit needs a comment and command to run and as far as I mean I don't want to I try to not explain stuff with great confidence when I don't know the full story so I will just delegate it to advance do you know you have installed right now something known as rpm what rpm is it means Ruby version manager and the reason we have that is because obviously it's new versions of Ruby come out we want to block you to one particular version so that nothing changes Wadley I'll give you like update of your phone or update your laptop and then something broke like it doesn't work anymore you update your apps and stuff if you maintain one single version you can create consistency in terms of the predictability and reliability of your program now when it comes to bundle exec you have what is known as global gems so that is your default gem base set that's installed on your computer sqlite3 active record cry is will install it to your global version of relief sometimes what can happen is what your app thinks the JEM version it wants to use it will default to your global sometimes if you want to force it to use the jet version specified in your gem file you will use bundle exec which is short for a bundle executor so bundle execute forces your app to use the exact version of the gem specified in your jump-off to create thank you thank you cool great that's the benefits of having a lead in the in the room or go boom awesome okay so now we've already you already know that you should never change anything in the schema what should you do if you want to change anything in the schema by a team yeah you want to you want to just create another migration that will change everything or you can just do it in a clumsy way horrible way the way that I just did but please don't quote me on that and let's let's I will just cut it out from the recording I'm killing your health yeah you can roll back of course and then remain great there are a few different ways in general you should never touch the schema that's what I want you to remember forever even once you're done with school never touch the schema okay awesome so now we want to set up associations in classes right because if we only have associations in our database that doesn't do much because we want we also want to make sure that our models our classes now know about each other cool so let's do that okay so doc what about joke I can show you wait where is it here is the domain model so what Association should go here has my name user dope cool how do you feel about it mm-hmm I see Arianna go ahead aha what would you like to explain it would be explained I can but I also want everyone to be verbalizing their worries so before I do that I also want I am also expecting some criticism on the syntax here what are you what are you guys I'm how I wanted to choose different intonation anyway so what about the syntax here something is missing yeah so SS are missing yeah because if you have many then you don't have many singular you have many plural so user jokes has many users through user jokes right just like remember that Ruby speaks English so plural plural okay so now I really appreciate your question actually so let's go back to our colorful model so thanks to the magic of active record you can and also thanks to how Ruby is wonderfully thought-out you can access the joke through any any given joke through the take you can access any given joke through their association to a user so user has many user jokes let's say that would be the favorite yeah so I let's say I like three dogs those are my three favorite jokes and now I can also if I want to I don't know just print out all the drugs I I can thanks to this association I can access these drugs I can access their content I can access the length of their content I can access their IDs if there was more yeah I could access even more information let's say if I had another model I'm gonna say something that will totally blow your mind you can actually create a chain in which you would be use their user joke joke also or something something and you can just append all this as methods in Ruby right so you can do user does jokes and then you will see all the drugs that user favorited yeah both do that Shammi what not in this lecture but I'm super happy to see to a few later on actually to remember that I'm just gonna post I'm gonna just gonna part it a packet old way as see Asians awesome but something that I don't know some people like to know but when I was when I was learning active record that's really confused me so if you are prone to confusion just cover your ears and if you were super curious about code this is the moment for you so has many is actually a method right that accepts arguments so I will just show you something so this sentence that you see here this is just actually the argument to the hezmana method that accepts that has the first symbol of users and then there is a hash right but now in order to simplify it for everyone around the world they just created this syntax which is pretty awesome right because it's more readable cool so how do you feel about this hezmana has many threads through like this is I feel great this is like huh and this is oh no I'm drowning mm-hmm okay some people don't feel and I think I will take it I will look hope that this is not despair manifesting itself okay so job is done what I like to do I will do so I like to start with one one corner and then go to the other corner so now I'm gonna do Joe OH so I started with dogs I will go use two users now and basically I am just copying pasting right however here it has many what jokes yes and now the more difficult part the user dog so what what will go here I encourage a person who hasn't yet spoken on the subject how about you borrow code name called comment what would you write here so you're seeing and so you see that you have a user that has many user dogs and you have a dog that has many user dogs perfect belongs to what is it singular or plural yes because it belongs to this one and belong store doc yeah so usually I am against putting people in spot however just know that if I am doing that I have a firm belief that this is a question you can manage and you know however if you are a person who can get lots of anxiety from that tell me after the lecture so that next time I'm not gonna do that to you okay cool so we already have the associations now how can we check whether our associations work and ideas aha we can create some seed data okay so let's go to seat oh we already have some sick data wonderful so now this is some seed data that you don't have to track ups that you don't have to trust that it's good right also what the hell is this user dog late night coding laziness okay so I would always even if if it's your seed data I would always check whether what you have here is what you expect with what the Ruby is expecting you to have okay so doc drug should have a content joke should have a content and the content should be a string awesome user should have a user name Mazen yes where is this lowercase an idea Eric did you want to say I said that she opened yes five points exactly so in general if you were writing your your seat data it's good to either write everything up case or the workings and then on the other side put in safeguards that will prevent you from you know running into a you know user who inserts their name with every second letter being capitalized right so we're gonna do that and then this is something that I just learned I didn't know that you can do it this way usually if I would create a user joke right I would do user coke dot create user and I mean whatever user ID right and then actually do you know how to do that so what should I write here if I want the user ID to be this user Arianna and maybe a good practice will be already written to write user ID okay and then joke ID okay does everyone understand where this comment came from exactly so as far as I I have the faint recollection and we can check it in a moment I have a faint recollection of that in maybe active record would save you if you just wrote user right here that it would still work however I I thought I would always tell you to be as specific as possible just for this is a good practice and it will save you like if you develop this kind of habit it will save you your life in like react and JavaScript and now why we are doing that we are saying basically we are turning Ruby to go to user variable that's here yeah and we can actually maybe okay maybe just not to have too many user we have a user here you have user here you here here so maybe I would just create I will just change the name to user 1 just so that it's something different right and joke okay cool so what we are saying we're telling Ruby first hey Ruby create a user with this username and save it to a variable user one and then we are saying hey Ruby go to create a new joke with this content and save it to a variable Tok one and then we are telling Ruby create user dog with the user ID pointing to find this variable the user that is saved to this variable and go to their ID now we're we want to create a connection to dot ID because this is how we decided to create the drawing right so as you see the user dogs require us to create to include a joke ID and user ID okay and because of that we need to provide it Eric how is it going oh yeah oh yeah you tell me so now it's not it's not not not here what would you write here do you have an idea and then like a faint idea or anyone I mean right because you see I am again telling Ruby because I want I want this this to this joke and this user to be connected yeah Mazen favorited this this is a test joke that's a great sense of humor imagine okay so this is painful joke on the earth so basically I tell Ruby to go to the variable joke one and we draw from that joke one the ID and in this way the connection will happen yeah because this is a discuss already been created Derek I see they can be they can be totally different because let's say if I created let's do joke too and then this is again a test joke so let's say Muzzin so this this joke and he thinks oh this is the best so actually we just want to we want to point this joke ID to the second variable and it doesn't matter what what's gonna be the idea here we don't need to know it we we Roop you will know it because we saved this whole joke to a variable so we will look for the variable and then it will be like okay so now give me the ID it takes a little bit of practice we'll do that and there was Shamy yeah exactly yeah so because of that we don't have to write an ID here this is Ruby Ruby's doing which is very awesome because sometimes this will be 5065 and I'll keep track of that yeah and right okay cool and so usually I would create a user joke like that however watching another lecture I just I just learned that you can do it this way that you can show this joke one into the jokes of the user I kind of don't find it very intuitive but it's here if you want to if you want to ever play with that right so now we have the seed data what should we do I heard already seed yes reg DB seed and look at that the readme is setting you for failure with the spaces okay I corrected it sorry so now how do we know that it worked because it was saying anything exactly okay so now okay so now if we want to check whether it worked or not I actually wrote it right remember sitting does not give you any output so no news onion on your console is good news so now we want to check whether with seated correctly however in order to run the console we need to first create such a task now I've already created the task so there is a reg file here you can put in here any kind of task you want you can set I don't know you can create a task rake let's say break hello and then the output would be just hello right so you can play with that it's kind of funny we're not gonna do it now but I created a test console I'm gonna shorten it to see so now in theory when I call Rick see the console should pop up and it did yeah so what it does it basically just starts the pry and it activates the active record okay so what do we want to check most probably but I don't know I was told to always talk to do that unless you are working in rails I don't know I'm gonna I'm gonna check do you get the Rick console from snatch Roger because I wrote I wrote the rake console you know in rake file but maybe I don't know I will check it and tell you okay so we already have we are in the console so what do we want to check anyone yeah maybe let's start with user all we want to see whether all the users are created and if you had more than one it's always good to see that last just to see who was the last person you created right so now we already we see that Mazen exists and so now we can do joke that all right to check the dogs we see two dogs it's always good to do that last ok and now Jonathan what did you say oh sorry I thought I'm sorry I thought something I thought something something else I misunderstood you so we see that user joke has been created right maybe let's also check what's gonna happen if we call this so you see undefined local variable or method user one so why do I get it in the console if I see clearly that this variable is here an idea okay Jonathan user one is saved on seeds file right and so if I created if I put in binding dot pry in the seeds file I will be able to work on that however on the console I can only work on the database stuff so if I want to see that user dot all dot for example first that's gonna be also Everlast ha ha the drugs cool so coming back to your question Riana a minecraft in your name correctly ok um you see thanks to the associations thanks to has many through we don't have to do user dot all that first now we are getting just the first user dot user user jokes so those are the favorite yeah dot first dot joke maybe yeah thanks to the fact that we set up our has many through we can just simplify to user all first jokes isn't this awesome okay so now because of the fact that you see that there are two favorites you see that both of these methods worked right I also want you to I want to show you something that many people don't know about but it's pretty awesome so do you did you know that if you do you know what's gonna happen if I write user dot create with a bang then I was the difference I'll show you what's the difference let's dump yeah so if you create it if you write it with a bang you will get exactly the error why it didn't work right I made it on purpose yeah so now look here oh okay well I don't know but usually maybe it works only as the logger in the CLI in general so it's in general it is better to I've learned that it's maybe it's only when you are working already with JavaScript or react if you are if you write create with a bank it will give you a very specific error what exactly didn't work oh sorry it's it's from rails on because you have in rails validations and stuff but which will give you very specific errors so I jumped the gun but you can already you can already get used to the fact that if you want to make your life easier you can just the right user dot create with a bank and in this way in rails and in JavaScript later on when you have rails back-end it will give you very specific errors but for now it doesn't make any difference for you so it depends it know it suffer it depends on the on the method like for example there's a method G sub and G sub with with a bank will give you much problem I think will result in a string and just disable result in a different type so it depends on the it's always good it's actually a very good question is good to consult the dogs yeah do you have a name I see that your processing happens do you have any comment to that oh no I thought that your processing watch and what I'm saying no no okay good okay so we have our seed seed setup and we know that it works awesome our work is done now it's not so now that we are ready set up we can go into our outline so okay so we already set up our project I'm thinking where to go with you next so we have our migration our database tab okay so let me show you around our our app our folder right so as you see I already spoke about rake file rake file is for specific tasks that you want your app to specific commands that you want to include that's not necessary but you can play with it gen file is for the gems right DB you already know in config you have environment file that basically can see configs your whole application and database and everything now this line will be super important for you later on but I will I will just comment it for now it makes your CLI with visual any more pleasing less cluttered I will show you we are basically also requiring all just like Eric said from the folder app so we are saying that our our environment will work on SQLite it will use this database that is in under this address yeah database search that development OPP and that will only work on the files that are in the app folder cool we have a bin with runner what runner - it's just click create a new one second joke up and so it will run it will look for the because of the fact that we have the environment set up this way when we call run dot RB it will check in with the environment to know where to look for joke up what is this what actually is happening here yeah create a new instance of a joke up and send a saving it to a variable up and then what's this now you know that AHA you're basically calling a method run on there on the joke up right that you just created okay so now where is this joke up you may ask well it's here so it's you know there is very different many different ways to set up your CLI and I would suggest that you have one runner file and then for just firing up the application and one file one file for all the logic now within this file you can call other classes right but it's good to contain all the logic here now as you see this is not inheriting from it is not using active record how do we know that yeah it doesn't say this yeah it's not inherent from active record so because of that maybe we know that we'll be using a user so I will just create an attribute reader user is that the syntax like that come awesome oh so because of the fact that we have very limited time I decided I already wrote some kind of skeleton for the further up I will leave this comment here just so you know what's happening ok cool so now I'm a huge fan and not only me but majority of the develop Deaf world of creating as many helper methods as you need now the extreme version of that is sandy mess who says that each method should not be longer than five lines and each class should not be longer than 100 lines right and so if you need more than if you if your method is going to be a little bit fatter than five lines perhaps you want to create a helper methods we're not going to be doing it this way however this is the idea that you should strive for cool so we see that as a user I should be able to sign sign up login so I would say that once we do those two parts we'll have the break mmm exciting everyone woke up ok ok so cool so what do you see here are these just comments or what this can be they can be methods yeah so there's gonna once we let's actually run it so how do I run my app do you know Ruby what run dot RB no such file or directory yeah because we look we're in the lecture kit yeah so we need to go into the folder of bin and then now this may seem obvious but wait until you're tired and you're trying to run the file and you cannot ok so we are getting an error on Joe Kapp line 6 and the fine local variable or method welcome that's correct now we're gonna have private method I used to I'm very used to writing in atom okay so what is it gonna be called welcome okay so now what do you want the welcome to say okay so how about how the holiday isn't this wonderful day for some jokes okay and then maybe Oh spoiler alert you can use this so this will freeze the code execution for a second so that the lines will happen one by one also outputs how about that I have a question you will see what it does put haha cool so now if I run the run then what's gonna happen huh yeah isn't this a wonderful day for some jokes okay let's do yeah now let's make this bigger and that's clear it how about that yeah so the difference is that if you do sleep see and this is the amount of time right it will freeze the code execution for this amount of time and this is a new so that the ha has happened exactly this at the same time that's not true there was a tiny tiny difference but you can't see that right but basically if you want to have the kind of illusion of the CLI talking to you just you sleep semi yeah let's see let's see it we'll get food - all right yes but it's just a better practice to put anything you think should be just access how how to how to explain it with grace advanced how would you explain with grace why do we need private methods why - why is it good practice to put methods in private I'm just being lazy here yeah I also maybe not because we don't have that much time so in general it is not huh it is more sense when you get Tamar to get me a private you'll see an example right now it's very abstract I decided to introduce it just so that you are not shocked when you see private methods later on a hard question how do you end everything underneath is private what is private what is public what's political we're gonna part because we don't have time okay basically this is those are the methods only accessible for this class not from the outside and so let's say from the rather I won't be able to call I don't know joke up don't welcome because it is hidden yes yeah below private yeah so you should have a key private and then all the private methods so the private everything that will go under private will be private right cool so now we have okay so now I just hinted here that we'll have log in our sign up so how do we want to ask the user whether they want to log in or sign up do you want to want to log in or sign or sign up oh yeah Sing Sing app would be better sign up okay I'll sign up and now how do we how can we make sure that we remember the your answer or that actually they can type get strong right here everyone is excited about it so how about we save it to a variable answer it equals gets chomp yeah and now let's just do codes now in order to check you can either throw in binding dot pry or you can just put answer just to see whether what what has been saved is what you thought you saved right so let's see hahahaha do you want to log in or sign up yes oh and it outputs exactly what I thought it would output right you can also just throw in binding dot pry now you can think oh but this is basic why would I test it oh please don't trust yourself just yeah cool so now so now on like high level what do we want to help if they say something that will prompt users to act right okay and then basing on the choice we would we want to either sign up or login cool so this should be here so now how can we do you want to sign up a login or sign up so basically they do because they fired this app so how about we just say give us your name to give us your username what's your your name I don't know to sign up I had our login that's not the best the best phrasing that we'll figure I will fix it later on but that's basically what we want them right we want we want them to give us their name but now okay so before we go into actually acting on this how would you what what do we want to do if we sign up so we want to create a new we don't have passwords here so let's say it's gonna be user equals user dot new and then user name it's gonna be answer yeah okay and now and now for login what do we want to do oh maybe let's don't create out sir and then with you logging what do we basically want to do yeah yeah we're gonna get to the logic but like on the yeah user name and that's answer right okay so I exactly look at you giving spoilers like that that's true so we could we could go the long way right so we can say if let's say that gonna be user think I'm just gonna call that your name yeah so if so huh so we could so basically we could go a long way saying that if this user exists if this user check in the database whether this user exists if it doesn't exist then create one right so that will be the long way however we can also do just create or I think it's find or create by right find or create by username and in this way we can just do we can keep it as it was and save this to user okay cool so now how about we throw in being binding dot pry just to see whether our user has been created isn't this a wonderful name for such a day for some jokes so I'm gonna write Madson cool so now what you see user has been loaded it has been found in the database this is the SQL speaking to you and the code didn't break and we are in binding dot pry how am I gonna check whether a new user was created or whether this this user was found well for example user that all that last will tell me whether how many users there are and whether the new user was created yeah so now I can check also I saved you this user that was just found or created to a user variable so I can do user I see that user is Mazen and it's his ID is 1 and the last user is Silvia you can even go face here and do compare at user to user at all dot last and it's true that means that a user was created and if it's false user was not created and Maslin was found right cool so we know that our methods works but does it work what's gonna happen what why can it not work I will tell you yeah so without this cool so now I'm gonna do the same what am i how am I gonna check whether a user was found or created I do user dot all last because I don't want all 70 users that we already created threat Masum and now I'm gonna check yeah and now that's not that's ID 3 but actually Mazen was we know that it was his idea was number one so that it's not really perfect right so what we want to do we want to down case it we want to down case it because we we that's how we write our seed data right and then you can also down case it here however I'm a strong believer of that every single method should have one job so if you are creating an entry in database this should be the only job creating an entry not doing math not doing any kind of manipulations of string right ok and now let's just make sure that we also welcome the the person let's say maybe let's do sleep maybe let's do one and then put all righty so what what am I gonna put here if I want to call the person who move their name okay let's see all righty yeah so what should they put username username yeah and now username is downcast so we want to title eyes it how do you what there will be method for that I see a strong presence of of pre-work I really like that yeah it works and now you may see you may say oh my god this is giving us so much headache this SQL and that's what I told you this is why you need this line because now if we run it yeah it doesn't give you damn I mean stuff okay oh my the time yeah it's like okay so you know what so let's do will do the break and then so this is the setup of the of the CLI however we didn't get into the interview working with api's so I will look for another room and there's gonna be a second part of this so yeah so in five minutes I will let you know what we're we are doing the part two so that other group can actually use their own as well okay thank you so much
Info
Channel: Sylwia Vargas
Views: 2,365
Rating: undefined out of 5
Keywords: Ruby, ActiveRecord, CLI, tutorial, command line application, flatironschool, coding bootcamp, 100daysofcode
Id: uSJFQaRjpvk
Channel Id: undefined
Length: 58min 54sec (3534 seconds)
Published: Fri Dec 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.