Rails 3 Generators: An Introduction

Ben Scofield, Former Viget

Article Category: #Code

Posted on

Like a lot of people, I've been watching the Rails 3 development process, but I hadn't turned any focused attention on it until the beta release last week. I've been playing with it a little, and one of the first things that struck me were the changes to the way generators work. Unsurprisingly, given the extensive changes made throughout the framework, generators look a lot different now, so I thought it might be useful to explore the new status quo in a series of blog posts.

The Lay of the Land

Let's get started by looking at the way things are on Rails 2.3.5. Here's the (relevant portion of the) output of script/generate in a current Rails app:

 Installed Generators Plugins (vendor/plugins): ... Rubygems: ... Builtin: controller, helper, integration_test, mailer, metal, migration, model, observer, performance_test, plugin, resource, scaffold, session_migration 

If you run rails generate in a stock Rails 3 app, you get this:

 Rails: controller generator helper integration_test mailer metal migration model model_subclass observer performance_test plugin resource scaffold scaffold_controller session_migration stylesheets ActiveRecord: active_record:migration active_record:model active_record:observer active_record:session_migration Erb: erb:controller erb:mailer erb:scaffold TestUnit: test_unit:controller test_unit:helper test_unit:integration test_unit:mailer test_unit:model test_unit:observer test_unit:performance test_unit:plugin test_unit:scaffold 

Whoa, that's a lot – and it's a lot more readable, thankfully. Once you get past that, though, a lot of the options are the same. In fact, all of the old Rails generators are still present (though some of them act a little differently now), and we've got a set of new ones, too: generator, model_subclass, scaffold_controller, and stylesheets.

The other obvious change is the addition of the ActiveRecord, Erb, and TestUnit sections. These are part of the new, more modular construction of the framework, and each can be swapped out for an alternative (e.g., DataMapper, RSpec, or whatever) (whatever means Haml, by the way). That pattern also shows up at a lower level – for instance, the TestUnit generators create fixtures out of the box, but you can swap that out for FactoryGirl factories with minimal effort.

The component-specific generators can be run independently (which is very useful, it turns out), but they're also invoked by the top-level generators. That means you've got significantly more control over the fine behavior of your generators than you did in 2.3.5.

The Plan

That's about it for the overview. Over the next several posts, I'll be looking at a few generators at a time: first, the most familiar ones from 2.3.5 (e.g., model, resource, etc.), moving on to the less commonly-used ones (metal, performance_test, etc.), and wrapping up the top-level generators with the ones added in Rails 3. Along the way, I'll dive into aspects of the component-specific generators, as well.

Related Articles