If your Rails application uses a database (and most do), there’s one more installation step you may have to perform before you can start develop- ment.
Report erratum
RAILS ANDDATABASES 22
Rails works with the DB2, MySQL, Oracle, Postgres, SQL Server, and SQLite databases. For all but MySQL, you’ll need to install a database driver, a library that Rails can use to connect to and use your database engine. This section contains the links and instructions to get that done. Before we get into the ugly details, let’s see if we can skip the pain alto- gether. If you don’t care what database you use because you just want to experiment with Rails, our recommendation is that you try MySQL. It’s easy to install, and Rails comes with a built-in driver (written in pure Ruby) for MySQL databases. You can use it to connect a Rails application to MySQL with no extra work. That’s one of the reasons that the examples
in this book all use MySQL.1 If you do end up using MySQL, remember to
check the license if you’re distributing your application commercially. If you already have MySQL installed on your system, you’re all done. Oth-
erwise, visithttp://dev.mysql.com, and follow their instructions on installing
a MySQL database on your machine. Once you have MySQL running, you
can safely skip ahead to Section3.6,Rails and ISPs.
If you’re still reading this, it means you’re wanting to connect to a database other than MySQL. To do this, you’re going to have to install a database driver. The database libraries are all written in C and are primarily dis- tributed in source form. If you don’t want to go to the bother of building a driver from source, have a careful look on the driver’s web site. Many times you’ll find that the author also distributes binary versions.
If you can’t find a binary version, or if you’d rather build from source anyway, you’ll need a development environment on your machine to build the library. Under Windows, this means having a copy of Visual C++. Under Linux, you’ll need gcc and friends (but these will likely already be installed).
Under OS X, you’ll need to install the developer tools (they come with the operating system, but aren’t installed by default). Once you’ve done that, you’ll also need to fix a minor problem in the Apple version of Ruby (unless you already installed the fix from Lucas Carlson described in the sidebar on the preceding page). Run the following commands.
dave> # You only need these commands under OS X "Tiger"
dave> sudo gem install fixrbconfig
dave> sudo fixrbconfig
1Having said that, if you want to put a high-volume application into production, and
you’re basing it on MySQL, you’ll probably want to install the low-level MySQL interface library anyway, as it offers better performance.
Report erratum
RAILS ANDDATABASES 23
Databases and This Book
All the examples in this book were developed using MySQL (version 4.1.8 or thereabouts). If you want to follow along with our code, it’s probably simplest if you use MySQL too. If you decide to use something different, it won’t be a major problem. You’ll just have to make minor adjustments to the DDL we use to create tables, and you’ll need to use that database’s syntax for some of the SQL we use in queries. (For example, later in the book we’ll use the MySQLnow( ) function to compare a database column against the current date and time. Different databases will use a different name for thenow( ) function.)
The following table lists the available database adapters and gives links to their respective home pages.
DB2 http://raa.ruby-lang.org/project/ruby-db2
MySQL http://www.tmtm.org/en/mysql/ruby
Oracle http://rubyforge.org/projects/ruby-oci8
Postgres http://ruby.scripting.ca/postgres/
SQL Server (see note after table)
SQLite http://rubyforge.org/projects/sqlite-ruby
There is a pure-Ruby version of the Postgres adapter available. Download postgres-prfrom the Ruby-DBI page athttp://rubyforge.org/projects/ruby-dbi.
MySQL and SQLite are also available for download as RubyGems (mysql
andsqliterespectively).
Interfacing to SQL Server requires a little effort. The following is based on a note written by Joey Gibson, who wrote the Rails adapter.
Assuming you used the one-click installer to load Ruby onto your system, you already have most of the libraries you need to connect to SQL Server.
However, the ADO module isnot installed. Follow these steps.
1. Find the directory tree holding your Ruby installation (C:\Ruby by
default). Below it is the folder\Ruby\lib\ruby\site_ruby\1.8\DBD. Inside
this folder, create the directoryADO.
2. Wander over tohttp://ruby-dbi.sourceforge.netand get the latest source
distribution of Ruby-DBI.
3. Unzip the DBI distribution into a local folder. Navigate into this folder,
and then to the directory src\lib\dbd_ado. Copy the file ADO.rb from
Report erratum
KEEPINGUP-TO-DATE 24
this directory into theADOdirectory in the Ruby tree that you created
in step 1.
The SQL Server adapter will work only on Windows systems, as it relies on Win32OLE.