ScreenSteps Live: A Case Study on Ruby on Rails - Part 2 - Figuring Out the Code
In my last post about developing ScreenSteps Live with Ruby on Rails (which was far too long ago) I talked a lot about what led to me getting started with Rails development. In this post I want to talk about what were some of the things about Ruby and Rails that I found really useful and what things were really confusing.
Once again - a little of my background. When I started this I had a music degree from the Berklee College of Music, knew some basic HTML and was at least competent at using CSS thanks to the book "CSS: The Missing Manual", which I can't recommend enough.
So I was by no means a programming expert. Concepts such Classes, Modules, ActiveRecord, View Helpers, Partials, Arrays, and Hashes were all totally foreign to me. I had done some database work several years ago where I got to dig into SQL commands a bit. This background at least gave me a good understanding of how relational databases worked.
The Stuff That Confused Me
Here are some of the things that initially confused me about Ruby and Rails:
Block notation
It looks something like this:
@posts.each {|p| p.some_method}
For some reason that totally confused me. Once you figure it out it is wonderful and very powerful. But as I read books and searched the internet I could find all these things that mentioned blocks but didn't define what they were or how they worked. I think that I will do a post soon about the important things I wish I had known about blocks but I don't have space for that here. Now that I understand them I can find plenty of information on them. Isn't that always the case?
Ternary operators
url.blank? ? "Preview not available" : url
That type of stuff totally threw me for a loop. All it is doing is evaluating an expression and returning a value depending on the value returned by the expression. It is short hand for an if-then statement. But those ? and : are very intimidating when your code vocabulary is limited.
Array Operators
posts.collect {|p| p.title }
posts.select {|p| p.title == "My Post"}
I didn't like that stuff at all, mainly because I wasn't comfortable with arrays yet. That compounded with the block notation had me dong a lot of head scratching.
The Stuff I Liked
I loved all of the ActiveRecord stuff. I knew how painful it was to write SQL. Getting all of those methods and extensions with such little effort was heaven on earth. It was so easy to operate on database records. Of course there were many gotchas that I wasn't aware of. But I got most of the concepts around ActiveRecord pretty quickly. Except for polymorphic associations. That took awhile.
I also loved the whole MVC (Model View Controller) thing. Separating all of that logic from presentation just made sense.
ERB was also wonderful. I know a lot of people don't like it but I only find it is a problem if you don't make use of partials and view helpers enough. I had only really done native HTML before so having access to variables, partials, etc. was fantastic. I was able to produce pages very quickly.
The Most Important Thing I Wish I Had Known When Starting Out
REST
If you don't understand REST you need to understand REST. Initially I had no idea what REST was and the code I had inherited was not RESTful at all. My controllers became a mess. I have recently been in the process of moving everything in our app to a RESTful setup and everything is so much cleaner. I have found that I end up with a lot more controllers that are much smaller. Everything becomes so much easier to manage and named routes are great to work with. Understanding RESTful principles has produced the biggest improvement in my code and my productivity.
My Resources
Starting out I used a couple of resources early on that were very helpful:
- "Agile Web Development with Rails" - This is a great book. I just wish it would go more into the "rubyisms" I mentioned above.
- Occasional questions to the programmer who had started the project (though I tried my best to not treat him as my personal Rails tutor and I made sure to pay him for all of his time.)
- Railscasts.com - In my opinion this is the single greatest Rails resource on the web. Ryan is so good at focussing in on a principle or a topic. His screencasts are always the right length, detailed enough without going too long. I have learned so much from these and still refer to them regularly.
- Peepcode - These screencasts are also very good though they tend to be a little long for my taste. I would prefer that they were broken into smaller segments but that just may be because I am a bit ADHD. They also offer a lot of PDFs that are very good as well.
- The web - There is a ton of stuff out there. Unfortunately a lot of it assumes you know some basics which I was lacking when I started. I never found a good resource that said, "Here are the basics you need to know about Ruby and then the rest of this stuff will make sense to you".
The one thing that I wish I had started using earlier was O'Reilly's Safari Books Online. What a great resource. There are quite a few Rails books in there as well as some good Ruby references. My life would have been much easier had I discovered this sooner.
That's it for this post. In my next post in this series I will talk about deploying our Rails app.
