September 2, 2008

Say it with me… “Rails Is Not A Programming Language”. Thanks! It’s easy to forget sometimes that all the magic of Rails is actually riding on a foundation of Ruby. Rails is a “Web Application Framework” that is written in Ruby. Ruby actually IS a programming language, Rails is a set of classes/packages designed to do a lot of the tedious work typically involved in writing a web-based application.

Ruby is very extensible, owing much to the fact that it is basically compiled on-the-fly. This means that Ruby can pull in chunks of code as needed and integrate them as new functionality or to augment/override even base functionality. Usually, these additional pieces of functionality are in the form of “Gems” (get it… Ruby… Gems). Well, Rails itself is actually just another Ruby Gem (technically a collection of Gems)! You install Rails (and update it going forward) using the same Ruby commands you would use to install any other Ruby add-on. I suppose that Rails seems like a language because it actually becomes your interface to Ruby, wrapping Ruby “the language” inside of itself.

The idea behind Rails is to abstract complexity whenever possible, integrate existing 3rd part tools whenever practical, and provide structure, intuitiveness, and design simplicity wherever probable. Installation of Rails automatically includes the base packages (ActiveRecord, ActionController, ActionView, etc.), a back-end ruby web server called WEBrick, and the JavaScript libraries “Prototype” and “Script.aculo.us” to do the pretty AJAX stuff in the browser. So, with all of these typically independent pieces neatly wrapped up into one integrated package, it is easy to see why so many people initially mistake Rails for a programming language.

The Rails framework does a great job of things like:

Giving you the feel of working in an integrated environment. It does much of this by replacing traditional HTML-only files with a pseudo-HTML file into which you can combine Rails objects and variables and Rails method calls directly (in-line) with your HTML, CSS, and JavaScript.

Integration of JavaScript (Prototype and Script.aculo.us) into Rails helper methods hiding some of it’s complexity

Taking care of routing web requests

Creating secure communication sessions

Generating e-mail

Ability to generically output to many formats (XML, HTML, etc.) with almost no additional coding

Automatic integration of a testing environment for unit and application regression tests

Where it really shines is helping you create database applications, since it will help you do things like:

Handle database transactions

Performing data validation

Automatically show/update related records

Record locking/detection of stale data

Generating automated testing routines

Performing SQL without writing pages full of SQL

Maintain the state/values of variables and objects between your web pages and your code

Performing database record caching inside easy-to-use memory objects.

Rails “encourages” (almost demands) standardized folder structures, file naming, and method naming conventions. This may feel limiting to some at first, but the payoff is extraordinary. It infers a level of self-documentation to every project. It makes it much easier for new developers to join a development team at any stage. It also makes technical documentation and blogs such as this one easier to write because so many things can simply be assumed about the development environment.

The naming standards, request routing, and other basic concepts in Rails afford you some additional cool abilities when it comes time to move/change your application/server/database:

instantly move your applications between development, test, and production

duplicate/rename an entire application without making any code changes

change your back-end database “from” just about any database technology “to” just about any database technology with no code changes

change your server-side

In Summary…

Rails is not a language

Ruby is a language

“Ruby on Rails” is the official name

“Rails on Ruby” would probably be a more accurate description, but it just doesn’t roll off the tongue as well or sound quite as sexy.

Can you write a Ruby on Rails application without learning Ruby? It is a testament to the depth of the Rails framework that you actually could write any number of fairly simple web applications without learning a tiddle of Ruby. You obviously are still “using” Ruby, even if you don’t really understand it. Doing this, however, would not be a very wise undertaking. It would be like planting your very first garden without knowing the first thing about dirt, water, sunlight, or (dare I say it) bugs.

March 5, 2008

A Model Is A Class

A “Model” is a Class that deals with the manipulation of data. Classes allow the combination of data and methods in one place. Because it is a full-blown Class, it can have any number of its own variables and methods.

The Model class is the perfect place for data validation for a Rails system. It actually makes more sense to store data-based logic here than in the database (assuming that other systems are not monkeying with your data). This is one of the reasons most Rails programmers don’t use database constraints (one of the big reasons is ActiveRecord doesn’t like it either).