Thursday – June 30th – One More Day Left of Bootcamp

Twas the last day of June and not many were at the Dojo

Today started off pretty blah. I was the first in my cohort to arrive at the Dojo today and by about 9am there were only four of us total here. Another person trickled in in the afternoon.

At any rate, I had a productive day today! I spent some of my morning writing a blog post that was long over due. Once that was out of the way, I started looking at some Rails associations from the platform. Afterwards, I went back to the LAMP stack to see if there were any projects to I could recreate in Rails. I noticed one – involving Products (showing and removing products)…that sparked an idea to add login and registration to it and instead of any product…create books.

At 3:30pm I had completed the login/registration/logout, show page(user dashboard) that displays a list of books that are added by users —- The user who created a book can edit (updating the title, description, quantity or price) or remove the book. There also another table that can display the books that the user purchased. And finally a user can cancel an order by deleting the row in the associated table.

Here’s some of my work in the console:Screen Shot 2016-06-30 at 3.35.38 PM

Snippets of my Books controller:Books Controller

User Dashboard Controller: User Dashboard Controller

Routes file:
Routes File

I have to say, I’m pretty excited about what I created in about 5 1/2 hours (with a couple breaks in between). It’s rather exciting because three months ago I wouldn’t have been able to get that far – Now, because I have a better understanding of queries, making information appear on view pages and getting the data submitted by the user; I am able to press forward. I’m excited to put these skills to good use.

The Last Days of Bootcamp

The bootcamp is just about over and I haven’t blogged in a bit.

Let’s start with a little bit about last week.

About Last Week:

Last week was mainly about planning to take the belt exam on Friday. I wrapped up a couple of assignments at the beginning of the week and by the middle of the week I was ready to work on the belt exam prep test. If I never mentioned it before – the belt exam is a test taken at the end of three weeks of every stack to test our knowledge of the stack. During the test, which are given 4 1/2 hours to complete, we get a wireframe of what we need to do and instructions of what our final product should expect to have. There’s always a login/registration page, which after a successful attempt should log a user into their page which includes things like where you can add an item such as an event, a show page – which shows more about a particular event and sometimes a table of all the events added by every user.

We took our exam Friday afternoon and I have to admit that I felt I did better at my first attempt than I have on past exams. I managed to finish deploying within the next our the test is over and was on my out the door by 7pm.

This week…so far:

The week started of with a small group in my cohort. It was our last week, which is time we spend working on a project, catching up on past material we may have didn’t grasp or couldn’t get to or retaking our belt exam if we felt the need. On Monday, there were about 5 people here from my cohort. I worked on updating my personal website and going over some stuff on my exam from Friday and making the finish product look nicer.

On Tuesday there were a couple more people that trickled in and I had a tough time concentrating because I’m very exciting for the end of the program. I received my belt exam results back and found out that I received a red belt – which in my case was 1.3 points away from a black belt. With that news, I decided I’d head home since I knew I wasn’t getting any work done. At first, I told my instructor I wasn’t going to retake but after my 30 minute train ride home, I decided to regroup and to retake on Saturday.

On Wednesday, I didn’t go in to the dojo – I wanted to stay at home to concentrate in silence so I could go over what went wrong on my exam. One of my main issues is that I had extra information in my table that didn’t need to be there – it was as if the query I had been used was looping multiple times and I found out later that that was the case. To fix this issue, I wracked my brain for hours before taking a break to run some errands since I was at home. When I returned I felt revived and went straight to my computer and got to work. Turns out I needed to to do a join and select the columns I needed from each table so I could just loop through them and get the data I needed one time instead of multiple times. Here’s the query I used:

@lend = Lender.joins(:histories).select(“lenders.first_name, lenders.last_name, lenders.email”).select(“histories.amount, histories.borrower_id, histories.lender_id”)

This query joins the Lender and History tables and selects the lender’s first and last name and email. It also selects the histories amount and id’s of both the borrower and lender. Once this was set into place, it was easy to loop through this to get the information for my table as below:

  <% @lend.each do |help| %>

      <% if help.lender_id == session[:user_id] %>

       <tr>

        <td><%= help.first_name %> <%= help.last_name %></td>

        <td><%= help.purpose %></td>

        <td><%= help.description %></td>

        <td><%= help.needed %></td>

        <td><%= help.raised %></td>

         <td><%= help.amount %></td>

      </tr>

    <% end %>

  <% end %>

I felt pretty accomplished after this – It may seem like a small feat to anyone else but it took a long process to come up with the query to use. I used my rails console to try out different queries in order to figure out which one would give me the data I needed.

Controllers and Views in Rails:

This post is long overdue! We learned about Controllers and Views last week.

If you do something like this to make a new controller: rails g controller products index new

this is what’s called rendering by default what this means is that the method will attempt to load a view by the same name of the method (Rails automatically creates a view when you create methods in the Controller). The only way to prevent this behavior from happening is to render another view or redirect to another method.

To render another view: Specify using ‘render filename’ or ‘render folder/filename’ – if it’s in a different view folder. If a user registers to your page and the registration failed, then you want to render a page, this allows the page to display an error message and doesn’t save anything to the database. Rendering allows the error message to be displayed in an @instance_variable to display the view page.

The second option is redirect_to: redirects the browser to another controller/method…sends another HTTP request. redirects_to doesn’t actually load another view, it calls another controller/method to render a view. Use redirect_to when a user logs into your site and redirect them to another page because you don’t want to submit an HTTP request.

After a render and redirect_to: the code after those statements are ran.

render: works the same as var_dump and die() that we used in PHP

Passing variable to the view in Rails: Just put an @symbol in front of the variable name in the Controller that you want to make available in the View.

I completed a few assignments to day: All were pretty simple – straight the point. Most notably the Random Name Generator and the Survey Form all which we have done before in different languages so once you’ve seen it once, it’s pretty easy to tackle. Plus, these assignments are merely to get our feet wet in the language and be comfortable assigning instance variables, session data and flash data plus being about to show information in the views.

First Week of Ruby on Rails

We started Ruby last Monday. I have to admit, I wasn’t  fan of it, it was pretty loose language in that I felt like the rules could be bent quite far and it seemed like anything was possible. There were lots of functions used and some were representative of doing the same thing like .select and .collect. I was also not the biggest fan of not using curly braces and semi-colons. We resettled around with Ruby for a couple days learning how to declare functions, use all the built in functions and utilizing classes.

We learned of TDD – Test Driven Development and I can see right now how important this will be and how much more I’m going to have to practice at it. The platform for Coding Dojo explained on particular test: expect(project.name).to eq(“Project Name”)

but I later found out, by looking online that there were far more tests to include. It’s so much information that after we go through the Rails section, there is full section solely for TDD. I’ll have to go back through the module before we move to the full TDD section which will probably be the close to the end of next week.

Then we learned Rails…Now, this was a bit more cooler in that you can do a lot by not really tying a lot…This could be dangerous once we really know what we’re doing :).

We learned how to navigate the Ruby console, we installed the ‘hirb’ gem which allows the information received from queries to show up in a handy table that’s easy to read. I really wish there was a way to declare ‘hirb’ in every gem file instead of having to declare it each time but it’s okay, I’ve pretty much gotten used to declaring it by now and enabling it in the Ruby console each time you exit and re-enter the console.

We went through the Models section and did a lot of work in Ruby console using Validations, Relationships and Migrations. The validations are far more easier compared to any other stack we’ve used – PHP or Javascript, although PHP is close second for me. This simple line of code in your class can require the first and last name through presence: true and also requires the length to be in between 2-20 characters: validates :first_name, :last_name, presence: true, length: { in: 2..20 }

Relationships are fairly easy in Rails, so far we’ve learned about referencing another model, the has_one, has_many relationship, polymorphism and self joins

Self-join – Many to many to itself. i.e. User and Friend == Friendship -belongs_to :friend, foreign_key: “friend_id”, class_name: “User”. I used these two queries to find the Users that weren’t friends with the first user:

others = User.select(“friendships.user_id as Person”, “friendships.friend_id as Friend”, “users.first_name as First”, “users.last_name as Last”).joins(:friends)

others.where.not(“Friend = 1″)

Polymorphism: A table that could be used for multiple relationships.We used the Blog/Post/Message assignment to create polymorphism: Blog/Post/Message =  Each user can have many comments, Each blog can have comments, each Post can have comments, Each message can have many comments. Example schema: commentable_id, commentable_type

After the first week, I’m more comfortable with the fact that we don’t use semi-colons and curly braces. In fact, the language looks quite nice without all the closing braces, plus it reenforces the fact that tabs and spacing does matter and makes code easier to read. All in all, it doesn’t seem to bad and I am warming up to Rails!