The In’s and Out’s of my First Bootcamp Project
My third week in at Flatiron School, we were put into pairs and told we were ready to take off our training wheels and build our first project. After a few weeks of learning some Ruby basics, SQL, and ActiveRecord, we would have to execute a CLI application using CRUD. Now, after I heard this I thought to myself “this really is going to be easy, these labs we’ve been doing with SQL and now ActiveRecord comes in and basically does everything for us. This ain’t nothin!” …. Well I may have down played that a little.
Erik and I decided on the idea of creating a command line dream journal. Like most journals, you can write, edit, and delete an entry but you can also track certain patterns of dreams. We figured it would be great to know what type of dream it was, whether it was recurring, how many hours you slept, as well as how much of the dream you remember? This way the user would be able to find entries by certain attributes.
The environment setup is probably my favorite part- being an organizational freak, I LOVE structure and making sure everything is in its place. So installing the correct gems and creating the schema is a piece of cake.
Here you can see how our schema is set up:
Now like I said the environment set up isn’t where I get mixed up at. I will run a rake db:migrate all day and make a class here and there. The problem comes after that. Thankfully, ActiveRecord makes it easy to relate objects but it can still get a bit tricky when implementing these relationships in code. Just like in real life if you chose the wrong relationships, things can take a nasty turn and that is true in coding life as well. Now, there are a few different types of relationships:
- Belongs to
- Has Many
- Has Many through
Let’s start with the most simple: Belongs to, where one object belongs to another (easy enough? I mean sounds pretty possessive if you ask me but hey, I’m not to here to judge). Next, we have the Has Many, let’s think of a woman and her shoes… a woman can have many shoes but a shoe only belongs to one woman. And finally, the Has Many through which you can sort of think as the connector or joiner of these objects. So let’s say we spent a little too much money shopping — our credit card declines buying our next pair of fabulous stiletto heels that means we have to call capital one. I have a relationship with capital one through this maxed out credit card. My credit card is the middle man and I have many banks through a credit card. Most of our information about the bank will come from the credit card. Below is a an example of a class with these relationships listed…
OKAY — now that we’ve labeled and explained these relationships. lets move on to getting our CLI Dream Journal App running! First thing, is to establish a user because like we stated before, we want a user to create, read, update, delete (aka CRUD) entries and do some other cool things! After we initialize a user, there is a lot of “puts” and “gets.chomp” going on (actually throughout your whole app, you might get sick of typing puts and gets.chomp). Then after getting a main menu set up (where we listed options of what the user could do in the app such as update entry or create entry). We will have to set up those basic crud methods and param-setters. Remember when we set up our tables and each class had certain attributes? Well we have to do just that, set the value of each of these instance variables. Below is an example of a method we created to set up these values…
While setting up these methods and assigning values to each variable, we knew certain values (like hours_slept?) would correlate with a custom method where a user could tell the entry how many hours they slept that night. Below is how we implemented the method:
Now this is just the start!!! Once you get these basics done, there is nothing stopping you from creating the world’s BEST CLI APP! Don’t forget, that ActiveRecord is your best friend, and makes it easy to return interesting data back to your user and help you transform your app!