Connecting to MS SQL Server with Ruby on Rails


If you’re running the Rails app on a Windows box you’ll first need the the DevKit toolkit in order to build gems:

Regardless of platform you’ll need to install the ODBC Binding for Ruby and SQL Server 2005 and 2008 Adapter For ActiveRecord. Add the latter (at least) to your project by adding config.gem "activerecord-sqlserver-adapter" to your environment.rb config section.


On Linux we’ll need the FreeTDS libraries in order to talk to the MS SQL. You’ll need to add an entry to the freetds.conf file specifying how your database can be reached. Be sure to confirm the installation to make sure it’s possible to connect to the database.

To bridge the gap between Rails and FreeTDS you’ll need to add your database configuration settings to two additional files, /etc/odbc.ini and /etc/odbcinst.ini.

In /etc/odbcinst.ini add something to the effect of:

Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib64/
Setup = /usr/lib64/
CPTimeout =
CPReuse =
FileUsage = 1

Make sure the Driver and Setup paths point to the actual libraries.

In /etc/odbc.ini add something to the effect of:

Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = SERVER_NAME

Then, in your database.yml file:

adapter: sqlserver
mode: ODBC
username: USER
password: PASS

Customize the all-caps strings as needed, except the mode, which really is ODBC.

No Comments

No comments yet.

Comments RSS

Sorry, the comment form is closed at this time.

powered by WordPress     themed by Mukkamu     presented by     everything else by steve hulet