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( 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 =“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!