I recently came across Sam Ruby’s Rails Confidence Builder post and used it as a model to test out my connection to a legacy SQL Server database. I’m undertaking a new project with Mel at Upstate and wanted to verify connectivity before diving into the Rails application. I’m using Actual Technologies SQL Server Driver because it’s easy to set up and use, making it worth the 30 bucks. One thing I noticed right away was that the attribute names come back in the same case as they are in the table definitions, so I’ll have to translate those to lower case.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
require 'rubygems'
require_gem 'activerecord'
ActiveRecord::Base.establish_connection(
:adapter => "sqlserver",
:mode => "odbc",
:dsn => "the_dsn",
:database => "db_name",
:username => "userName",
:password => "some_password"
)
class Customer < ActiveRecord::Base
set_primary_key "customer_id"
set_table_name "customer"
end
Customer.find(:all).each do |customer|
puts customer.NAME
end
|
Another cool thing is that in TextMate all I have to do is plop the code in a new buffer and press CMD-R to run it.