I must confess that Rails makes me feel stupid every time I use it. The accepted truism about the framework is that it boosts your productivity like no other. Unfortunately people forget to tell you that there is second part to this statement that goes something like this: “once you learn to think the Rails way”. It really forces a certain mindset upon you, and deviating from it means that you are actually working against the framework, rather than having it do the work for you. It takes a little while to get used to it, and there are moments when you have a great scaffold thing going on with bunch of interacting tables/entities, but you sit there for 20 minutes trying to figure out how to make a simple pull down menu (aka select statement) which would let you choose the foreign key from the other table. You could do it the hard way, but it turns out that it is astonishingly simple:
<%= collection_select :foo, :bar_id, bar.find(:all), :id, :bar_name %>
This will create a select statement looking something like this:
It really took me some digging to figure that out – mainly to realize that what I needed was in a ActionView::Helpers::FormOptionsHelper class. A lot of the online tutorials simply gloss over little details like that – for example, the importance of helpers, which I now know are pretty damn convenient.
Then there is that whole Rails 2.x vs. Rails 1.x debacle. The two are not entirely compatibile, and there are significant differences in the way they work. Needless to say, when version 2.0 instantly invalidated every single Rails book on the market by removing the active scaffolding which everyone was using in the initial examples. I already got burned on it once, and now I was hit with it again, only from the other side. When I decided to install rails on Gutsy, I did what any reasonable Ubuntu would do:
sudo aptitude install ruby rails mongrel
Few minutes later I was all set up and ready to go. Or was I? I gently issued a command like this:
script/generate scaffold Foobar foo:string bar:string
I got hit by some cryptic error about unknown string identifier or something among those lines. WTF? It took me few minutes of useless googling, and cursing to realize I simply had the old version of rails installed. I could have went along and simply use the nice active scaffolding for my project, but I figured if I am to learn this damn framework, I should probably use the latest and the greatest version. How to install it on Gutsy though? The answer is – via gems.
First, get rid of the 1.x rails installation if you actually have it on your system:
sudo aptitude remove rails
Next, install the new rails:
sudo gem install rails --include-dependencies
That should do it. I think it’s possible to downgrade back to 1.x if you remove rails via gem and then install it back via apt bur I haven’t tried it.
Also, small caveat – you may or may not need to update your gems package to do that. You can do it by issuing a command:
sudo gem update --system
Be warned that it will actually break the gem command itself. If you try running it, you will get the following error:
/usr/bin/gem:23: uninitialized constant Gem::GemRunner (NameError)
Why is this? Take a look at this:
$ ls -l /usr/bin/ | grep gem -rwxr-xr-x 1 root root 701 2007-08-24 01:18 gem -rwxr-xr-x 1 root root 785 2008-04-01 11:25 gem1.8 -rwxr-xr-x 1 root root 3201 2007-08-24 01:18 gemlock -rwxr-xr-x 1 root root 1778 2007-08-24 01:18 gem_mirror -rwxr-xr-x 1 root root 515 2007-08-24 01:18 gemri -rwxr-xr-x 1 root root 70 2007-08-24 01:18 gem_server -rwxr-xr-x 1 root root 1813 2007-08-24 01:18 gemwhich -rwxr-xr-x 1 root root 7947 2007-08-24 01:18 index_gem_repository
Apparently all the gem_* commands have been deprecated in the 1.x releases of rubygems. The version in gutsy repo is 0.9.4 which means it still uses them. The update command brings you to 1.1.0 release but unfortunately does not remove the old scripts from /usr/bin. So the original gem and gem_ commands are useless. Quick workaround here is:
sudo mv /usr/bin/gem /usr/bin/gem.old sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
You could probably remove the gem binary, but I simply renamed it, and then created a link to gem1.8 in it’s place. It works well enough, and if you need to do a downgrade later on, all the files are still intact.
[tags]ruby, ubuntu, gutsy, rails 2.0, rails, gems, rubygems, gems 1.10, gems 0.9.4[/tags]