Prev | Current Page 384 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


The initial setup work we need to do follows:
# Example working with Legacy schema
require 'rubygems'
require_gem 'activerecord'
ActiveRecord::Base.establish_connection(:adapter => "sqlserver",
:host => "mydbserver", :database => "testdb", :username => "sa", :password => "")
class Account < ActiveRecord::Base
set_table_name "members"
CHAPTER 7 ?–  WORKING WITH LEGACY SCHEMA 174
set_primary_key "Members_ID"
end
class Comments < ActiveRecord::Base
set_table_name "comments"
set_primary_key "Comments_ID"
end
class Direct < ActiveRecord::Base
set_table_name "direct"
set_primary_key "Direct_ID"
end
At this point, you might be wondering why we chose to use the set_primary_key method
instead of just setting the class attribute primary_key_prefix_type to :table_name_with_
underscore, since it appears that in all of our cases, our primary key is the table name plus an
underscore and the string ID. The answer lies in the fact that our column labels are of mixed
case. This is an important thing to note, because Active Record will force all column or field
names to be lowercase with a call to the Ruby downcase method unless you specifically tell it in
which cases not to do so.


Pages:
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396