Dr Nic

First look at rails 3.0.pre

This article is out of date in some aspects. See comments, and perhaps this summary of reading materials about Rails 3.

3.pre

Today I had my first look at rails 3.0.pre and below are the sequence of steps I had to take to create a rails 3.0.pre application, and get it’s generators to work.

Why was I looking at the top-secret, yet open-source Rails 3.0? Their generators are being migrated over to Thor and I wanted to see them in action. I was thinking I might migrate newgem to use Thor too.

Here’s a quick poke around of getting started and interesting things I found. Any hiccups and workarounds are meant as a guide to pre-pre-3.0 users/developers and not as a criticism of rails-core. Rails 3.0 is looking shiny and awesome.

NOTE: Since this is a “how to install and use” rails 3.0 edge, which is still in heavy development, this set of instructions might break. Let’s hope not.

Getting Started

As of today, you cannot install 3.0.pre from rubygems [1]. So, let’s install them from source. Which is handy, you might like to patch something.

$ cd ~/gems
$ git clone git://github.com/rails/rails.git
use_ruby_191 *

[*] If you are on OS X Snow Leopard I think you can ignore this. Otherwise, since you don’t have the 3.0.pre gems installed, you’re about to hit bump #1. Ruby 1.8.6 doesn’t have Symbol#to_proc but it’s required to create a rails app. This means you’ll need to be able to switch to another version of ruby temporarily if you’re on ruby 1.8.6 [2].

cd ~/Sites
ruby ~/gems/rails/railties/bin/rails

Oooh, look at all the new options! Some new ones are:

-F, [--freeze]             # Freeze Rails in vendor/rails from the gems
-O, [--skip-activerecord]  # Skip ActiveRecord files
-T, [--skip-testunit]      # Skip TestUnit files
-J, [--skip-prototype]     # Skip Prototype files

The -D, --with-dispatchers flags have been removed. --freeze isn’t new, but -F is.

So, to create an app, I dutifully used:

ruby ~/gems/rails/railties/bin/rails edgerailsapp -F

BAM! Fail. The -F option to freeze/vendor rails fails without the gems installed. So don’t use it.

ruby ~/gems/rails/railties/bin/rails edgerailsapp
ln -s ~/gems/rails vendor/rails

If you’re on Windows without the symlink command ln, then copy the downloaded rails source into vendor/rails.

Fetch Rails’ dependencies

Rails 3.0 source uses the new bundler project to describe its own dependencies. From Nick Quaranto’s article on bundler, get the latest:

cd ~/gems
git clone git://github.com/wycats/bundler
cd bundler
sudo rake install

Now, back in your app, you need to install some rails dependencies here too. It’s a good chance to see how you’ll bundle gem dependencies in the future.

$ cd ~/Sites/edgerailsapp

Change the Gemfile in your project to the following:

gem "rack",          "1.0.1"
gem "rack-mount",    :git => "git://github.com/rails/rack-mount.git"
gem "rack-test",     "~> 0.5.0"
gem "erubis",        "~> 2.6.0"
gem "arel",          :git => "git://github.com/rails/arel.git"
gem "sqlite3-ruby"
gem "rails", "3.0.pre", :git => "git://github.com/rails/rails.git"

Welcome to the future of gem dependencies for rails apps. Ultimately you won’t need to manually add these lines yourself. When rails is distributed as gems it will automatically install these for you, I assume/hope/guess. But for today, you seem to need them.

Now locally (within your app) install these gems:

$ gem bundle

If you get “can’t convert Pathname into String” then revert to ruby 1.8.X and reinstall bundler into your 1.8 gem cache.

Phew. Ooh, my god. Phew. Only now will script/generate work.

$ script/generate

For me, this outputs:

Please select a generator.
Builtin: app, controller, generator, helper, integration_test, mailer, metal, migration, model, model_subclass, observer, performance_test, plugin, resource, scaffold, scaffold_controller, session_migration, stylesheets.
Others: app_layout:app_layout, check_migration_version:check_migration_version, home_route:home_route.

The “Builtin” generators are the latest and greatest in Thor technology. Rails 3.0 no longer uses its own generator but is built upon Thor.

For example, our old favourite model generator works thusly:

$ script/generate model Post title:string --no-fixture
    invoke  active_record
    create    db/migrate/20091103030824_create_posts.rb
    create    app/models/post.rb
    invoke    test_unit
    create      test/unit/post_test.rb

Interestingly, --no-fixture isn’t mentioned in the usage information for script/generate model. It mentions the --fixture flag, but I had to guess that --no-fixture was also supported.

Hmm, I want to use rspec. So, let’s destroy these files:

$ script/destroy model Post title:string
      invoke  active_record
.../vendor/gems/dirs/rails/railties/lib/rails/generators/active_record.rb:14:in
  `next_migration_number': uninitialized constant ActiveRecord::Base (NameError)

Oh well.

What if I wanted to run rspec and cucumber generators, for example, against an edge rails app?

Rails 2 generators

The “Others” generators are my own local generators from ~/.rails/generators. Amusingly, instead of app_layout it is called app_layout:app_layout. Not surprisingly at all, if I try to run the rails 2 generator it fails:

$ script/generate app_layout:app_layout
[WARNING] Could not load generator at "/Users/drnic/.rails/generators/app_layout/app_layout_generator.rb". Error: uninitialized constant Rails::Generator
Could not find generator app_layout:app_layout.

Poop.

Note, I have rspec, rspec-rails and cucumber gems installed locally but I cannot see their rails generators above. Rails 3 doesn’t look for generators in the same way and old Rails 2 generators don’t work anymore.

That’s the news: every rails 2 generator is broken.

When I start to migrate some of mine I’ll post about it. In the meantime, José Valim has written some introduction thoughts on using Thor as a generator.

You can also probably learn about how to write rails 3.0 generators by looking at the source code for the new generators like rails, model (see main and test_unit), and scaffold/resource.

Finally, José Valim has a sample Rails 3 app with some vendored generators in it.

These are the things I’m researching now.

Summary

This article is long, mostly because rails 3.0.pre hasn’t been released as a set of RubyGems. If it had, then all the dependencies would be installed automatically.

It also introduces gem/plugin writers to the first upgrade issue: your current generators are neither discovered nor work by a rails 3.0 app. We’re all clever cookies, so here’s hoping we can figure out the upgrade path and that it’s simple enough to not be the topic of Dan Brown’s next book.

Footnotes

[1] Two portions of rails 3.0.pre are available as pre-release gems: activesupport (which is now very modularised and only loads up the parts that you require) and activemodel (which is shiny and new and hence completely safe for rails-core to release).

[2] There are two popular ways to have easy, non-intrusive access to alternate versions of ruby: rvm and ruby_switcher.sh.

Related posts:

  1. Instant new Rails applications with the App Scrolls When I start a new project I want to start...
  2. Using CoffeeScript in Rails and even on Heroku I’m pretty excited about CoffeeScript as a clean-syntax replacement for...
  3. Dead simple JavaScript Unit Testing in Rails Formats: Video/Screencast (410 Mb, torrent) | Video only (vimeo)...
  4. Rails themes can remember things I was getting annoyed at having to remember all the...
  5. Install any HTML theme/template into your Rails app Have you ever even bothered to Google for “rails...

36 Responses to “First look at rails 3.0.pre”

  1. Nice write-up. Going to give it a try tonight.

  2. [...] First look at rails 3.0.pre, see what’s broken. Hint: everything [...]

  3. [...] leave a comment » ttp://drnicwilliams.com/2009/11/03/first-look-at-rails-3-0-pre/ [...]

  4. José Valim says:

    Hey Dr. Nic!

    Thanks for the write-up, I want to address some concerns:

    1) –no-fixture isn’t listed because you can prepend –no or –skip to any option in thor. The guessing part should be fixed with proper documentation, that’s why writing a new generators guide is on my list to be solved before Rails 3.0.pre release.

    2) Yeah, freeze does not work without gems and dispatchers are deprecated;

    3) As far as I know, Rails 3.0 no longer supports Ruby 1.8.6;

    4) I will investigate script/destroy bug, thanks!

    5) In my sample app, it shows how to use rspec in scaffold. You just need to copy rspec generators in lib/generators and configure your environment.rb (in the future, they should be released with rspec gems).

    6) Yes, Rails 2.x generators do not work anymore. That’s why we do a different lookup than Rails 2.x, we now search for them inside lib/generators. The only exception is ~/.rails/generators, which will have both generators (for Rails 2.x and Rails 3.x) raising the weird naming errors. I will investigate if they can be solved somehow.

    Regards!

  5. Arg says:

    Can some tell to yehuda and josè that thor is unstable, bad, complicated (to manage) and … orrible?

  6. Anon says:

    Can some tell to Arg that his spelling is unstable, bad, complicated (to read) and … orrible?

  7. Botanicus says:

    Anon: exactly. http://twitter.com/botanicus/status/5388657768 I liked Thor before, but what the hell is wycats doing on it now, that’s horrible :(

    Ad Rails 3: For me it seems like it will takes ages to release Rails 3 (+ time for plugins), current Rails are – no offense – horrible, so Merb is apparently still the best solution. BTW talking about Merb, a lot of people is doing pretty good job on it, it seems it’s not dead at all!

  8. Ryan Bates says:

    Thanks for the writeup Dr Nic. I would love to see a followup article detailing your experience in creating a generator in Rails 3. Keep it up!

    @Arg, I haven’t tried Thor yet, but why do you say it is unstable/bad?

  9. Robert Rouse says:

    José is right. During the Rails 3 update at WindyCityRails (September in Chicago), Yehuda responded to a question by saying that 1.8.6 won’t be supported in Rails 3.0. It has some bugs (or missing features.. I forget) in it that are apparently just too much trouble to work around and maintain those hacks.

  10. Kieran P says:

    If you use ‘gem bundle’, then vendor/rails is completely ignored. So you can skip the vendor/rails symlinking step altogether. The basic process becomes:

    * git clone rails to rails-master
    * ruby rails-master/railties/bin/rails newapp
    * [edit newapp/Gemfile, comment out rails line, uncomment rails from git line, and save]
    * gem bundle
    * good to go!

    I have a shell script that tells you what to do. http://gist.github.com/225330

    Save, chmod +x the file, then off you go. It assumes you’ve already cloned git to rails-master.

    I have plans to add conditions to check for the rails clone, and make it if needed. And hopefully auto edit Gemfile and bundle, so one command and you’re set to script/generate.

  11. Dr Nic says:

    @valim + robert – the Symbol#to_proc code (which is the only bit missing that makes the rails command not work for 1.8.6) still exists in activesupport but wasn’t loaded. Again, probably just an issue because I didn’t have gems installed.

  12. Dr Nic says:

    I was told by Yehuda that there is a rake install command (it is currently missing a description so it doesn’t show up). I found I had to run rake install twice to install the gems from scratch (due to install ordering). The rails command works for 1.8.6 with the gems installed.

  13. Kevin says:

    I’ve ran through this on a Windows XP machine and am experiencing an error during the ‘gem bundle’ execution when the bundler tries to create a symlink at lib/repository.rb(line 166).

    Will continue to troubleshoot. Just wanted to shout out in case there are other poor souls that require it to work on Windows :)

  14. Dr Nic says:

    @kevin – my guess is to raise this issue the bundler project (specifically).

  15. José Valim says:

    Ok, I have some updates, based on the latest commits (http://github.com/rails/rails/commits) just pushed to master:

    1) –freeze option was removed, since all Rails apps come with Rails bundled by default (just check the generated gemfile);

    2) Confirmed, Rails 3.0 no longer supports Ruby 1.8.6;

    3) script/destroy bug fixed. So now feel free to customize your scaffold to user rspec and/or cucumber ;)

    4) Improved error message if you try to run a Rails 2.x generator.

    If you find any other bug, feel free to open a ticket on Lighthouse and assign it to me.

    Regards!

  16. Dr Nic says:

    @jos̩ Рawesome awesome awesome.

  17. Mike Riley says:

    Hello everyone,

    I struggled with getting this working so I wanted to share my experience in hope that it would help others if they come across the same issue. I followed the directions above, but was failing at this step:

    cd ~/gems
    git clone git://github.com/wycats/bundler
    cd bundler
    sudo rake install

    The error I was getting was:

    bundler-0.7.0.pre/lib/rubygems_plugin.rb:2:in `require’: no such file to load — bundler/commands/bundle_command (LoadError)

    So that you know more of the story, I am using rvm to switch my Ruby version between my default and 1.9.1. When I did a rake install or installed version 0.6.0 of the bundler gem it installed it in this location:

    ~/.rvm/ruby-1.9.1-p243/lib/ruby/gems/1.9.1

    This was causing the error of not being able to find the gem. It was not being installed in the correct location. It should have been installed in:

    ~/.gem/ruby/1.9.1/

    I fixed it by copying over the gem contents as follows:

    bundler-0.6.0.gemspec to ~/.gem/ruby/1.9.1/specifications/

    bundler-0.6.0/ to ~/.gem/ruby/1.9.1/gems/ fixed this issue.

    Hope this helps someone.

    Mike Riley

  18. [...] First look at rails 3.0.pre – Dr. Nic dives into getting started and using generators. [...]

  19. Postmodern says:

    Dr. Nic: RE your thoughts on migrating newgem to Thor, I’ve already migrated one of my projects code-generators to Thor::Group and Thor::Actions.

    http://github.com/postmodern/ronin-gen/blob/master/lib/ronin/generators/generator.rb#L30
    http://github.com/postmodern/ronin-gen/blob/master/lib/ronin/generators/platform/overlay.rb#L32

    It worked out pretty well.

  20. Ah, if you want to try a app using Rails edge, without having to bundle everything twice, I usually follow those steps:

    http://pastie.org/684600

    The point is to use your rails edge checkout environment instead of the one in your application. `rake dev` just create a fresh app using AppGenerator.

  21. David Backeus says:

    Just a quick note that though ruby 1.8.6 support is gone. Ruby 1.8.7 is still supported.

  22. Mike Riley says:

    Hello,

    I have been working on trying to generate any model or controller and I can’t get it to generate anything. For example I run the example above:

    script/generate model Post title:string –no-fixture

    and get the following error:

    vendor/gems/gems/activesupport-3.0.pre/lib/active_support/dependencies.rb:456:in `rescue in load_missing_constant’: uninitialized constant Rails::Generators (NameError)

    It doesn’t matter if I try a model or a controller. It always goes back to that error.

    Any thoughts on what I am missing?

    Mike Riley

  23. [...] work to get Ruby 1.9.1 working, now it is time to get the application set up. I started with Dr. Nic’s post and experimented a little. Here is how it [...]

  24. [...] Nic Williams escreveu um tutorial do Rails 3.0.pre, o embrião do aguardado Rails [...]

  25. I have posted some updated instructions on my blog, so just go for it if you want to try Rails 3.0.pre today:

    http://rvdh.de/blog/2009/12/18/trying-rails-3-pre-today/

  26. [...] As an aside, if you fancy having a go with the full pre-release (a.k.a. "pre") version of Rails 3.0, check out Dr Nic's slightly out of date but otherwise useful guide. [...]

  27. [...] dem vollen pre-release gehen (auch bekannt als "pre") Version von Rails 3.0, check-out Dr Nic's etwas veraltet, aber ansonsten nützlichen Leitfaden. <img src="http://feeds.feedburner.com / ~ ff / RubyInside? i = RqqYVinUa2g: QEytyqiVYac: [...]

  28. Kristian Mandrup says:

    I got it to work in ruby 1.9.1 using rvm (ruby version manager)
    I then tried with 1.9.2, but got this error:

    $ sudo gem install bundler
    /Users/kristianconsult/.rvm/gems/ruby/1.9.2/gems/bundler-0.8.1/lib/rubygems_plugin.rb:2:in `require’: no such file to load — bundler/commands/bundle_command (LoadError)
    from /Users/kristianconsult/.rvm/gems/ruby/1.9.2/gems/bundler-0.8.1/lib/rubygems_plugin.rb:2:in `’
    from /Users/kristianconsult/.rvm/ruby-1.9.2-preview1/lib/ruby/site_ruby/1.9.1/rubygems.rb:1136:in `load’
    from /Users/kristianconsult/.rvm/ruby-1.9.2-preview1/lib/ruby/site_ruby/1.9.1/rubygems.rb:1136:in `block in ‘
    from /Users/kristianconsult/.rvm/ruby-1.9.2-preview1/lib/ruby/site_ruby/1.9.1/rubygems.rb:1134:in `each’
    from /Users/kristianconsult/.rvm/ruby-1.9.2-preview1/lib/ruby/site_ruby/1.9.1/rubygems.rb:1134:in `’
    from :252:in `require’
    from :252:in `load_full_rubygems_library’
    from :372:in `const_missing’
    from /Users/kristianconsult/.rvm/ruby-1.9.2-preview1/bin/gem:15:in `’

    I tried to follow Rileys advice, but in my .gem/ruby there wa only a /1.9.1 directory, no 1.9.2 dir. An the 1.9.1 only had a cache dir inside it, no /specifications dir fx. Anyone knows how to get rails 3 and gem bundler working with ruby 1.9.2 or is it not supported yet?

    Kris

  29. Chris says:

    Is there a way to update an existing app to rails3-pre ?

  30. Dr Nic says:

    @Chris – @jm recently started a project to help with rails2 -> 3 migration. see here

  31. Ben says:

    Kieren P’s script above is pretty kick ass. He helped me get a newer version working… here’s the newer gist: http://gist.github.com/293298

    I had to gem unistall bundler (all versions) and gem install bundler –pre (per KPs instructions).

  32. [...] of the new bits of technology coming into Rails 3.0 is the all singing and dancing Gem Bundler . If you’ve ever found yourself in config.gem [...]

  33. Charles says:

    [...] of the new bits of technology coming into Rails 3.0 is the all singing and dancing Gem Bundler . If you’ve ever found yourself in config.gem [...]

  34. Bruce says:

    [...] As an aside, if you fancy having a go with the full pre-release (a.k.a. “pre”) version of Rails 3.0, check out Dr Nic’s slightly out of date but otherwise useful guide. [...]

  35. Kieren P’s script above is pretty kick ass. He helped me get a newer version working… here’s the newer gist: ankara oto kiralama

    I had to gem unistall bundler (all versions) and gem install bundler –pre (per KPs instructions).