This tutorial is meant to be used with rails 3 final version, ruby 1.9.2 final version too.
The purpose is not to install rails 3 or ruby 1.9.2, tutorials to do so are mentioned in my 2 earlier posts of August and September 2010. The idea here is to connect from an Linux Ubuntu 10.04 station (or server) where you have your rails application to a Windows station (or server) where your SQL Server 2008 database resides.
First, to connect your machine, you need to be able to perform a connection to your database. To do so, you'll have to install FREETDS and UNIX-ODBC.
1. Install FREETDS
sudo apt-get install freetds
2. Configure FREETDS
edit the file /usr/local/etc/freetds.conf
insert the following text:
[TDS]
host = NAMEOFWINDOWSHOST (or an IP address like 192.168.0.130 if you are not on a domain)
port = 1433
tds version = 8.0
3. Test FREETDS
tsql -S 192.168.0.130 -p 1433 -U username -P password
You should see a prompt if it works. Then you can go ahead and parameterize UNIX-ODBC
4. Install UNIX-ODBC
sudo apt-get install unix-odbc
5. Configure your ODBC
edit the file /etc/odbc.ini
insert the following text:
[192.168.0.130]
Driver = FreeTDS
Description = ODBC Connection via FreeTDS
Trace = No
Servername = 192.168.0.130
Database = MySQLDatabaseName
edit the file /etc/odbcinst.ini
insert the following text:
[FreeTDS]
Description = TDS Driver (Sybase/MS SQL)
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
CPTimeout =
CPReuse =
UsageCount = 1
6. Test UNIX-ODBC
isql 192.168.0.130 username password
The ip address here can be replaced by the name you have on top of /etc/odbc.ini (it's the name of your ODBC). (if you have a desktop version, you can also install ODBCConfig to edit the ODBC connection).
if it went correctly, you should be able to query your database with SQL (Warning, the output will look a bit messy but it's just a connection test after all).
The second part is to verify that the firewall on your network or machine allows a connection to your SQL Server. Usually you have to specify a rule in your firewall to allow the Unix machine to 'talk' to the Windows machine using the port 1433 (or any other port if you changed the default port for TCP communications in your SQL Server configuration).
The third part is entering (at last) the ruby and rails world. You will need to install RUBY-ODBC (at least version 0.99992) and the ACTIVERECORD-SQLSERVER-ADAPTER (at least version 3.0.0)
The current version that comes when you do
GEM INSTALL 'RUBY-ODBC' does not work with Ruby 1.9.2 because the STR2CSTR has been deprecated in Ruby 1.9.2. So you'll have to install this manually (details can be found in this
thread)
download the fixed version at
http://www.ch-werner.de/rubyodbc/ruby-odbc-0.99992pre3.tar.gz
uncompress it in 'ruby-odbc-0.99992pre3', go to the directory and install it using the following commands:
ruby -Cext extconf.rb
make -C ext
make -C ext install
Note: more details are available in the README file in the same directory.
Then install the activerecord-SQLServer-adapter
gem install activerecord-sqlserver-adapter
(or add it to your Gemfile).
Then edit your application and modify the config/database,yml file as follows:
development:
adapter: sqlserver
mode: odbc
dsn: 192.168.xxx.xxx # This is your DSN name (here the IP is the DSN name too).
username: testdummyusername
password: testdummmypassword
host: ILOVEMYWINDOWSPCNAME #not necessary
That's it. Now you can test your application to see if you can connect.
RAILS SERVER