Ruby on Rails - An Introduction

The best way to get started with rails (unless you discover some great tutorial website that I don't know about) is via the "Agile Web Development" book. I don't have the second edition, and a third edition is in the works in response to the changes that came along with rails 2.0.

People call rails "opinionated software", and that it is! You need to do many things the "rails way" or life with rails will NOT be easy for you. For example, a database table should have a plural name like doughnuts, but the class that provides an interface to the same table should be called Doughnut (with the capital). It is also a really good idea if each table has a field called "id". This may seem like giving up freedom (it is), but you gain so much that it is ultimately a big win ... but you do have to learn what is expected of you.

This "opinionated" nature of rails tends to steepen the learning curve. It also is what helps rails do so much for you once you learn how the game is played. The other thing that I imagine helps steepen the learning curve is the need to learn the ruby language. This was not an issue for me when I came to rails, since I was already well along and very enthusiastic about ruby (and still am). Trust me, time spent learning ruby will be well spent and rewarding.

Now for one of my peeves about the Agile book. It comes down to the way they handle this MVC (Model, View, Controller) business. They really need to provide some explanation of what is so great about doing things this way, whereas most of what they do is to indirectly scold you for cases where you might be tempted to violate this way of doing things. Hopefully this is addressed in a better way in the second and third editions of the book.

The new thing (as of Rails 2.0) is the new pet concept that they call REST. They explain that being "RESTful" is good for you (like eating your vegetables), and that it is deep "Computer Science" that would take too long to explain. My suspicion when I hear things like this is that it is most likely a simple concept (or handful of concepts) being made into a dragon via smoke and mirrors, and I wish they would stop all the silliness.

Speaking of Rails 2.0, be warned that lots of things changed in Rails 2.0, and much or most of what is out on the web deals with Rails prior to 2.0.

Model, View, Controller (MVC)

Dealing with this tripartite division of your project can be a pain in the rear. I just keep several editor windows open so I can just grab a tab and be editing the controller or the view in seconds (the model seems to need much less fiddling). The files for the model, view, and controller are all in separate directories, so I gets really annoying in a hurry to support this partitioning, but for a big project maybe it is worth it.

Here is my take on the MVC thing: The view is code that sends information to the user. A static website, has little more than a view. The controller gets involved whenever we have to do something in response to a user request (in a static website, it just selects the correct view). In a dynamic website, the model is the interface to the database (any server resident state).

Active Record

This supports the models used by rails. It is a nice object oriented wrapper around (in my case) access to a MySQL database. Active Record is great for writing ruby scripts outside of rails to fiddle with databases. Each database table becomes a class, each record (aka row) becomes an object of that class, and each column become an object attribute. For most things you never need to worry about writing SQL, although you can if you care to or want to. The find() method is the all important tool to fetch records.

Action Controller

This is the component that maps user requests (typically URL's) into calls on action objects. Even a trivial rails site would need some kind of controller to route the site URL to a view.

Action View

This is the component that you use to send output to the user. The bulk of the work is done by eruby, which lets you write the view as .rhtml files and escape into ruby at will to generate dynamic content. There is very nice synergy between database records and forms; this is where rails comes into its own, making this ordinarily onerous task almost pleasant.

Other stuff

There are other rails components, such as Active Mailer that I have never used.
Feedback? Questions? Drop me a line!

Ruby on Rails notes / tom@mmto.org