On a recent project I needed to test my application with lots of data so I remembered my good friend the Faker Gem from the Ruby world.
I didn’t know how to connect ActiveRecord to SQL Server, so I searched the webs and found this article by Ray Houston at Los Techies.
Unfortunately since the article was 13 months old, the Ruby libraries have evolved to not work that way anymore :) So after some head scratching I got it to work again.
How to connect ActiveRecord to SQL Server
- If you don’t have Ruby installed, grab the one-click installer from http://rubyforge.org/frs/?group_id=167&release_id=28426 and make sure you check the box to install rubygems.
- Open up your PowerShell or console, and run:
gem update ––system
gem install activerecord activerecord-sqlserver-adapter
*This should install all prerequisites gems as well.
- Grab my example from github:
http://github.com/alexmoore/activerecord-with-sqlserver-example/tree/master
- Run the CreatePersonTable.sql script to add the example table to your database.
- Open Config.rb and put your database connection info in.
- Run: ruby PersonFactory.rb and you should be golden!
In my next post I will show how to use the Faker gem and some statistics tricks to quickly mock up a plethora of test data.
-- Alex