August 31, 2006
Dumping a Table to Yaml
I decided I wanted all 356 rows of a table dumped to a fixture, but
CSV was a disaster. So:
require 'yaml' js = Journal.find_all File.open ('dump.yml', "w") do |f| js.each do |j| f.print YAML.dump ({"j_#{j.id}" => j.attributes}) end end |
(I actually did it from script/console.) The only caveat is I got a
line of “-” between each entry that I had to delete.
November 22, 2005
Using Active Record to Test a SQL Server Legacy DB Connection
I recently came across Sam Ruby’s Rails Confidence Builder post and used it as a model to test out my connection to a legacy SQL Server database. I’m undertaking a new project with Mel at Upstate and wanted to verify connectivity before diving into the Rails application. I’m using Actual Technologies SQL Server Driver because it’s easy to set up and use, making it worth the 30 bucks. One thing I noticed right away was that the attribute names come back in the same case as they are in the table definitions, so I’ll have to translate those to lower case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
require 'rubygems' require_gem 'activerecord' ActiveRecord::Base.establish_connection( :adapter => "sqlserver", :mode => "odbc", :dsn => "the_dsn", :database => "db_name", :username => "userName", :password => "some_password" ) class Customer < ActiveRecord::Base set_primary_key "customer_id" set_table_name "customer" end Customer.find(:all).each do |customer| puts customer.NAME end |
Another cool thing is that in TextMate all I have to do is plop the code in a new buffer and press CMD-R to run it.