This week Gerardo Santana has announced the first release of Rails/Informix, an Informix adapter for Ruby on Rails, and a new version 0.3.0of the Informix driver for Ruby. So nowseems like a good time to take them for a test drive.
Trying to let go of preconceptions, I resolved to followCurt Hibbs' Rolling with Ruby on Railstutorial to the bitter end to make a simple recipe application.
Documented here are mainly the Informix specific deviations from the tutorial. Note I used IDS 10.0 - the recommended version for Rails/Informix. My test platform was Windows to match the tutorial, so for any operating system specific paths and commands assume the equivalent for your OS.
Since the onlamp tutorial also includes instructions on installing MySQL, refer to Jean Georges Perrin's Installing IDS on XP article for an equivalent tutorial on installing IDS.
1. Install Ruby, and Ruby on Rails
The "One-Click" Ruby installer for Windows was downloaded from http://rubyforge.org/frs/?group_id=167. Installation is as simple as running the GUI and accepting the defaults. Once installed, add c:\ruby\bin to the path and Ruby on Rails can beinstalled by running:
C:\ruby>gem install rails --include-dependencies
2. Build and Install Ruby-Informix
Download the Windows or the UNIX package from http://rubyforge.org/projects/ruby-informix/ and unzip. On Windows (with the Informix Client SDK 2.90.x installed) set the Microsoft Visual Studio environment (vcvars32.bat), then build and install the driver:
ruby extconf.rb --with-informix-include=%INFORMIXDIR%\incl\esql --with-informix-lib=%INFORMIXDIR%\libnmakenmake installNote: on Windows only Visual Studio 6.0 is supported for building the 0.3.0 Ruby driver. The Informix CSDK version I used was 2.90.TC6.
3. Install the Rails adapter for Informix
Download the Informix adapter for Rails from http://rubyforge.org/projects/rails-informix/, unzip and (following the instructions in the INSTALL file) copy informix_adapter.rb to c:\ruby\lib\ruby\gems\1.8\gems\activerecord-*\lib\active_record\connection_adapters\.
Then "informix" needs to be added to the RAILS_CONNECTION_ADAPTERS line of c:\ruby\lib\ruby\gems\1.8\gems\activerecord-*\lib\active_record.rb.
Now Ruby on Rails is ready to use with IDS.
4. Following the Onlamp tutorial
The Rolling with Ruby on Railstutorial explains how to create a cookbook database in MySQL using MySQL-Front. An equivalent IDS database can be created in the data dbspace via dbaccess with the following Windows batch file:
echo create database cookbook in %INFORMIXSERVER% with log; ^create table recipes(id serial, title varchar(255), description varchar(255), ^ date date, instructions text, category_id int); ^create sequence recipes_seq; ^create table categories(id serial, name varchar(50)); ^create sequence categories_seq; ^ | dbaccess -Note: the database needs to be created with logging and for every table a sequence named after the table also needs to be created.
Because the "informix" string was added to active_record.rb as part of the Informix Rails adapter installation, the config\database.yml file for any new Rails application can now be populated with Informix specific connection details. My cookbook\config\database.yml looks like this:
development: adapter: informix database: cookbook@mydbserver username: myuser password: mypasswd
test: adapter: informix database: cookbook@mydbserver username: myuser password: mypasswd
production: adapter: informix database: cookbook@mydbserver username: myuser password: mypasswd
Where mydbserver corresponds to an SQLHOSTS entry on this machine. If a username and password, or the dbserver were not specified, a local database connection would be attempted with the credentials and environment of the user running the Ruby on Rails server.
With the exception of these data server specific parts the rest of the tutorial, including Part II can be followed verbatim.
The Informix adapter for Rails is working well with the latest Informix driver for Ruby. Give it a try, and remember the following points:
- On Windows build the 0.3.0 Ruby driver with Visual Studio 6.0
- Create a logged database for your application
- Create a sequence for every table
- Make sure any BLOBs and CLOBs you use allow NULLs
Once the basic Recipe application screens were working I was impressed with how Ruby on Rails can build a database enabled web application with just a few lines of code. Am I converted by convention over configuration into using plurals to name my tables from now on? Er, still not convinced. Comments explaining how wrong I am are welcome.[Read More]