in

 

Ray Houston

Connecting ActiveRecord to SQL Server

Disclaimer: I'm a Ruby noobie. I know nada about Rails. Please leave a comment if something is not correct or if there is a better way to do this.

At work, we're using Watir to drive a Silverlight application for some automated end to end testing. We needed an easy way to access the database from our RSpec test fixtures to make sure the proper setup data is put where we need it. I discovered that ActiveRecord can be used without rails and that all I needed to do was just install the gem. Typing the following seemed to do the trick for me:

gem install activerecord

I then found this which told me that I have install the SQLServer adapter separately like so:

gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org

It then also says that one must get the latest source distribution of Ruby-DBI from here and copy the file:

lib/dbd/ADO.rb

to your Ruby installation directory in the following place:

X:/ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb

After that I was able to create a simple test page to see if could actually get connected.

ActiveRecordTest.rb

require 'active_record'

ActiveRecord::Base.pluralize_table_names = false

ActiveRecord::Base.establish_connection(
    :adapter => "sqlserver",
    :host => ".\\SQLEXPRESS",
    :database => "MyDB",
    :username => "sa",
    :password => "sa"
)

class Customer < ActiveRecord::Base
end

Customer.find(:all).each do |cust| puts cust.Name end

This test selects all the customers and outputs their names.

Technorati Tags: ,

Comments

 

Nathan Zook said:

Nope, that's pretty much it.  Punch your ducks at the file level.  It's simpler than having the adapter installer do it for you. <cough>

May 12, 2008 2:55 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Copyright Los Techies 2007. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems