Ruby on Rails connection to SQL Server

Apr 14th, 2008 by Jonathan Cummins - Tags:

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.

You can leave a response, or trackback from your own site.

6 Comments

  1. Matthijs Langenberg says:

    Ah, but this will only work on Windows. If you want mac or linux support, you need to use FreeTDS and iODBC or unixODBC.

  2. j4s0n says:

    Good read and was easy. though I want to read something connecting to MSSQL from mac or any nix machine

  3. micah says:

    have you noticed that binary columns behave incorrectly with SQL Server + Rails? i have been stymied by this

  4. Michael says:

    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/activerecord-and-sql-server.html

  5. dietrich says:

    The install syntax didn’t work for me. I used:

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

  6. dietrich says:

    … dang en dash… that’s a double dash before ’source’

Leave a Reply

Collaborate.
Enable.
Succeed.

Contact

(888) 331-8520
4210 Beau James Court
Winter Park, Florida 32792 RSS Feed

Search

Popular Articles

Recent Articles