- Apr
- 14
- 2008
Ruby on Rails connection to SQL Server
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_hereuser_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_nameIt 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.
Share this post
Other posts you may enjoy
You can leave a response, or trackback from your own site.


4 Responses to “Ruby on Rails connection to SQL Server”
On April 14th, 2008 at 3:56 pm Matthijs Langenberg said:
Ah, but this will only work on Windows. If you want mac or linux support, you need to use FreeTDS and iODBC or unixODBC.
On April 16th, 2008 at 4:50 am j4s0n said:
Good read and was easy. though I want to read something connecting to MSSQL from mac or any nix machine
On April 17th, 2008 at 5:21 am micah said:
have you noticed that binary columns behave incorrectly with SQL Server + Rails? i have been stymied by this
On April 21st, 2008 at 7:48 am Michael said:
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