Tuesday, February 9, 2010

You are here: Home > articles > Ruby on Rails connection to SQL Server

Ruby on Rails connection to SQL Server

by Jonathan Cummins on April 14, 2008

Recently I needed an application to be able to connect to a SQL Server database and import data into MySQL. After reading a number of different articles on the easiest way to accomplish this with each one being different and still not letting me accomplish my connection, I figured I would run through the steps that I used to make Rails work with SQL Server.

First off, I had to install the active record SQL Server gem.

install activerecord-sqlserver-adapter—source=http://gems.rubyonrails.org

Next, get the latest source distribution of Ruby-DBI from: http://rubyforge.org/projects/ruby-dbi/ and copy the file:

lib/dbd/ADO.rb

to:

X:/ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb

Then in the your config/database.yml file place:

sql_server_database_name:
adapter: sqlserver
database: database_name
host: server_name or local_machine_name
username: user_name
password: your_pw_here

user_name has to be a valid login for the SQL Server DB that has connect and select permissions

Lastly, place in any model that is going to need to establish a connection to SQL Server:

establish_connection :sql_server_database_name

It is that simple. Unfortunately, I never found a set of instructions on accomplishing this that worked for me, I had to take a number of examples and through trial and error, find out what combination worked. Hope this helps.

Bookmark and Share
  • Max
    Here is a tutorial which explains how to use RoR with SQL Server 2008: http://www.codecapers.com/post/A-Ruby-on-Rails-...
  • Thanks for the link to the Max. There are many Windows peeps running Rails that need more resources.
  • Matthijs Langenberg
    Ah, but this will only work on Windows. If you want mac or linux support, you need to use FreeTDS and iODBC or unixODBC.
  • j4s0n
    Good read and was easy. though I want to read something connecting to MSSQL from mac or any nix machine
  • micah
    have you noticed that binary columns behave incorrectly with SQL Server + Rails? i have been stymied by this
  • We have two Rails applications that interface with MS-SQL. There are lots and lots of headaches with that combination but we're actually getting things to work. Check out this link for just one gothcha:
    http://backyardbamboo.blogspot.com/2008/02/acti...
  • dietrich
    The install syntax didn't work for me. I used:

    gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org
  • dietrich
    ... dang en dash... that's a double dash before 'source'
blog comments powered by Disqus

Previous post:

Next post: