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: ,

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Ray Houston

Ray is a software development leader and architect with 20 years hands-on experience. He enjoys finding elegant solutions to complex problems and delivering real value to customers. Ray is a speaker and contributor to community events.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

3 Responses to Connecting ActiveRecord to SQL Server

  1. Nathan Zook says:

    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.

  2. CF says:

    WHERE I CAN GET THE FILE “lib/dbd/ADO.rb”
    please check

  3. Ray Houston says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>