WDI week 4: Yammer, ActiveRecord, RSpec and user authentication18 Oct 2013
The pace of WDI is really starting to pick up now, so my blog posts are probably going to get shorter as I have less time to write words, and spend more of it writing codez.
Week 4 was a short week for us due to a national holiday on Monday, so we began on Tuesday with our first field trip, to Yammer. They're an enterprise social network, and it was pretty cool to visit my first real tech company. A few of their engineers spoke to us and showed us around, and had some useful advice for junior engineers on getting into the industry. Stuff like making sure you write readable code, asking lots of questions, writing bash scripts to help you get things done faster, showing you're keen to learn, and not being afraid to say you don't know.
We spent Wednesday going into more depth on ActiveRecord associations (the various relationships your database tables can have with each other). ActiveRecord is a technology that lets you modify your database with Ruby code, then run migrations which implement the changes for you. This means that if you're using Rails, you basically never have to write SQL queries. I guess that's pretty handy, but I do feel slightly like I'm cheating with all these things Rails appears to just do for you!
Wednesday was more RSpec. I'll admit testing has been one of the hardest things for me to get my head round so far. I think the problem is that as a beginner, you have so many roadblocks and things to stall over, things you struggle with and don't know how to do, that being told you should always write tests for code you barely know how to write in the first place seems slightly terrifying - just another thing to hold you back. I'm not sure if it's the way it was taught to us at first, but I just couldn't really grasp the point to start with. A lot of the beginner books and tutorials use pretty contrived examples (like writing tests for features of a robot or something) and although I understood the concept of testing, I still couldn't work out what you'd test in real world situations.
Some extra reading around, and I finally started to get it, after framing it in terms of automating things you might otherwise need a person to test. Things like 'what should happen when that button is clicked?' etc. Plus, this Nyan Cat RSpec formatter is reason enough for anyone to want to persevere with RSpec, surely.
On Thursday, we learned how to build our own user login and authentication system using the bcrypt-ruby gem. There are a few steps to the process which I might cover in another post sometime.
We ended the week learning about a couple more things to tidy up the views in our Rails projects, notably form helpers and partials. Partials are files where you store chunks of code you might want to use more than once. You can then 'require' them in the relevant places in your app to avoid writing the same code again, thus making your views more DRY (don't repeat yourself). Form helpers are a shorthand way of writing forms that you can use in ERB files. They then generate HTML when rendered, saving you the hassle of typing out the full code every time.