Using Active Record to Test a SQL Server Legacy DB Connection

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.

Advertisements

1 Response to “Using Active Record to Test a SQL Server Legacy DB Connection”


  1. 1 Mark Aufflick November 22, 2005 at 8:49 am

    I’m not quite clear, but it sounds like you are trying to get active record working on a sybase/MS SQL Server database.

    If that is the case then I’m very happy since that is what I was searching for! Please post a follow up if you have any patches – I’ll be happy to pitch in.


Comments are currently closed.



Pages

Categories

Copyright ©2008-2013 James E Orchard-Hays, All Rights Reserved

%d bloggers like this: