A noob and Ruby on Rails 2

Update: I just had to install the whole stack again, now apart from the gems the following is needed:
1.) Mysql drivers/libs/dev files, beats me which ones, I just installed them all in synaptic.
2.) OpenSSL libs/dev files, installed them too via synaptic.


This apt-get string should cover it:
apt-get install ruby1.8 irb1.8 libruby1.8 rdoc1.8 ruby-dev rubygems mysql-server libdbd-mysql-ruby libmysqlclient15-dev libmysql-ruby openssl libssl0.9.8 libopenssl-ruby libruby-extras libssl-dev rake zip

Gems: rails mysql rubyzip mongrel will_paginate magic_model_generator

It probably only works on Ubuntu as is has some Ubuntu specific meta packages.

—–

I’ve just started to try and learn RoR for real. I had read a few chapters in Agile Web Development with Rails on an earlier occasion and I thought it was great so I started to go through it, despite the fact that it’s covering an earlier version of Rails.

To begin with I couldn’t run rails as a command to start a project, I’m pretty used to that now, adding this to /home/henrik/.bashrc and restarting the terminal takes care of it:

export PATH=$PATH:/var/lib/gems/1.8/bin:

And then I was able to do:

rails project -d mysql

I’m a noob and lazy too so I just use my XAMPP install when it comes to the database. My config/database.yml looks like this under development:

development:
  adapter: mysql
  encoding: utf8
  database: project_development
  username: root
  password:
  socket: /opt/lampp/var/mysql/mysql.sock

It was not until now that I ran into some real trouble. Since I’m new at this I don’t know what types there are, what is a varchar for instance, a string or maybe text, is a float float or double?. Therefore my strategy was to simply create the structures in phpMyAdmin and then somehow create the models automatically from the database information. That turned out to be somewhat messy. The below info also applies if you’re sitting with some kind of legacy monster.

I found a good writeup by Sean Lynch on how things are now in Rails 2. It seemed I was not going to get around the need to specify types after all through the new way Rails is doing scaffolding (which is awesome if you know what types to use).

During the search for some automatic scaffolding script that would read the field names of a table and use them with the correct type I found the magic model creator by Dr. Nic. What a bonus! I ran it right away:

./script/generate magic_model

Still that didn’t solve the basic problem of automatically creating the views from our tables. Eventually I ended up doing it myself but during my search I found a nice scaffolding script by Jeremy Evans, which I think is mimicking the way it worked in Rails 1. That is not what I wanted though, which is automatic generation of static scaffolds, not dynamic.

Anyway I first do for instance ruby script/generate scaffold Package, that will generate a completely useless empty view, it will create the basic controller though, so I just put this in the index method:

Package.columns_hash.each do |field, info|
  puts field + ":" + info.type.to_s + " " if field != "id"
end

Running /products will generate the info I need in the terminal, simply copying and pasting that gives me:

ruby script/generate scaffold Package label:string cost:decimal

After that I deleted the basic scaffolding and reran with the above line. I know, in this case the payoff is small but imagine a table with 50 different fields, then we’re talking. If I had had a massive legacy database I would have tried to automate the above procedure somehow, but I don’t so it works for me, it doesn’t involve too much manual work and I got to learn ActiveRecord types from the output.

Related Posts

Tags: , , , ,