Ruby on Rails connection to SQL Server
Apr 14th, 2008 by Jonathan Cummins - Tags: sql
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.
You can leave a response, or trackback from your own site.
Ah, but this will only work on Windows. If you want mac or linux support, you need to use FreeTDS and iODBC or unixODBC.
Good read and was easy. though I want to read something connecting to MSSQL from mac or any nix machine
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/activerecord-and-sql-server.html
The install syntax didn’t work for me. I used:
gem install activerecord-sqlserver-adapter –source=http://gems.rubyonrails.org
… dang en dash… that’s a double dash before ’source’