Los Techies : Blogs about software and anything tech!

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: ,
Kick It on DotNetKicks.com
Posted May 03 2008, 11:13 AM by Ray Houston

Comments

Nathan Zook wrote re: Connecting ActiveRecord to SQL Server
on 05-12-2008 2:55 PM

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>

CF wrote re: Connecting ActiveRecord to SQL Server
on 04-24-2009 4:41 AM

WHERE I CAN GET THE FILE "lib/dbd/ADO.rb"

please check

Ray Houston wrote re: Connecting ActiveRecord to SQL Server
on 04-24-2009 8:02 AM

@CF It looks like they changed the packaging from when I needed this, so I'm not sure.

Ben Ridout wrote re: Connecting ActiveRecord to SQL Server
on 03-02-2010 3:32 PM

You can find ADO.rb in version 0.1.1 of ruby-dbi here:

 rubyforge.org/frs

File: dbi-0.1.1.tar.gz

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

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