WeinstalledtheMySQLdatabasedriverin theinstallation steps atthe startof this chapter.Thisisprobablygood enough whileyou�fregetting toknowRails. If so,you can safely skip toSection 3.7, KeepingUp-to-Date, onpage 42.
If you�fre still reading this, it means you want to connect to a database other than MySQL. Rails works with DB2, MySQL, Oracle, Postgres, Firebird, SQL Server, andSQLite.For allbutMySQL,you�fllneed toinstall adatabasedriver, alibrary thatRailscan useto connectto and useyourdatabaseengine.This section contains thelinks andinstructions toget thatdone.
The database drivers are all written in C and are primarily distributed in source form. If you don�ft want to bother building a driver from source, have acarefullook onthedriver�fsweb site.Many timesyou�fll .nd thattheauthor alsodistributesbinary versions.
7. However,youmaynothavetodothecompilingyourself.it�fsoftenpossibleto.ndprecompiled librariesforyourplatform.
Report erratum
Prepared exclusively for Jordan A. Fowler
RAILS AND DATABASES
Ifyoucan�ft .nd abinary versionorifyou�fd ratherbuildfromsourceanyway, you�fll need a development environment on your machine to build the library. UnderWindows,this meanshaving a copy ofVisualC++.UnderLinux,you�fll needgcc andfriends(but these willlikely alreadybeinstalled).
UnderOSX,you�fllneedtoinstallthedevelopertools(they come withthe oper-ating system but aren�ft installed by default). You�fll also need to install your database driver into the correct version of Ruby. In the installation instruc-tions starting back on page 34 we installed our own copy of Ruby, bypassing the built-in one. It�fs important to remember to have this version of Ruby .rst in your path when building and installing the database driver. I always run the command which ruby to make sureI�fm not runningRubyfrom /usr/bin.
The following table lists the available database adapters and gives links to their respectivehomepages.
DB2 http://raa.ruby-lang.org/project/ruby-db2 Firebird http://rubyforge.org/projects/.reruby/ MySQL http://www.tmtm.org/en/mysql/ruby Oracle http://rubyforge.org/projects/ruby-oci8 Postgres http://ruby.scripting.ca/postgres/ SQLServer (seenotesaftertable) SQLite http://rubyforge.org/projects/sqlite-ruby
There is a pure-Ruby version of the Postgres adapter available. Download postgres-pr from theRuby-DBIpage at http://rubyforge.org/projects/ruby-dbi.
MySQL andSQLite are also availablefordownload asRubyGems(mysql and sqlite, respectively).
Interfacing to SQL Server requires a little effort. The following is based on a note writtenbyJoeyGibson, who wrote theRails adapter.
Assumingyou usedthe one-clickinstallertoloadRuby ontoyour system,you alreadyhave most of thelibrariesyou needto connect toSQLServer.However, theADOmoduleis not installed.Followthese steps(courtesyofDanielBerger):
1.
Wander over to http://rubyforge.org/projects/ruby-dbi, andget thelatestdis-
tribution ofRuby-DBI.
2.
Openacommandwindow,and navigatetowhereyouunpackedthe ruby-
dbi library.Enter these commands:
c:\ruby-dbi> ruby setup.rb config --with=dbd_ado
c:\ruby-dbi> ruby setup.rb setup
c:\ruby-dbi> ruby setup.rb install
TheSQL Server adapter will work only on Windows systems,becauseit relies onWin32OLE.
Report erratum
Agile Web Development with Rails
Mulai dari awal
