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:
to your Ruby installation directory in the following place:
After that I was able to create a simple test page to see if could actually get connected.
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.