Connecting a Rails app on Windows to SQL Server
I thought that connecting a Ruby on Rails application on Windows to an SQL Server database would be easy, just a sort of setting up an ODBC data source and the database.yml file properly. However, some issues not excessively documented in the internet have been turned up. So I‘ve written a small guide that may be of help to anyone trying to do the same:
(1) For this to work properly, first you need to install the DevKit. Just download the executable from RubyInstaller and run it. If it’s installed on C:\DevKit, for example, then follow the next steps:
cd C:\DevKit
ruby dk.rb init
ruby dk.rb install
(2) Install the ruby-odbc gem for accessing ODBC data sources:
c:\>gem install ruby-odbc
(3) Install the activerecord-sqlserver-adapter gem. Be sure to install the same version of your Rails framework. You have more info about it here.
c:\>gem install activerecord-sqlserver-adapter --version=2.3.5
(4) Create an ODBC data source. If you’re running a 64 bit Windows System, don’t use the ODBC Administrator Tool from the Control Panel because a 64 bit ODBC data source won’t work. Instead, open the 32 bit ODBC Administrator Tool this way:
c:\>cd C:\Windows\SysWoW64
c:\>odbcad32.exe
Select a default database on your DSN. Select English as the default language, although you’re not using English (I had problems with date formats using Spanish).
(5) Edit the config/database.yml configuration file:
production:
adapter: sqlserver
mode: ODBC
dsn: your-odbc-data-source-name
host: your-sql-server-host
username: an-sql-username
password: the-username-password
Ok, it’s time to create your database:
rake db:migrate RAILS_ENV=production
That’s it. Well, almost. If you have any problems with Decimal numbers (I had) just change the decimal type to float on your migration files, and recreate your database.