Prev | Current Page 366 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

So this attribute is a great way to work with
that convention without having to override the primary key setting in every model (via the
set_primary_key method).
table_name_prefix
This is an attribute that you set directly on the ActiveRecord::Base class that allows you to
define the prefix to be used with all tables throughout all of your Active Record instances. The
default prefix used when you do not set this attribute is an empty string (which equates to no
prefix being used).
CHAPTER 7 ?–  WORKING WITH LEGACY SCHEMA 165
The following example states that all of our tables should have the string "draftwizard_"
attached as a prefix:
# Setting the table_name_prefix
ActiveRecord::Base.table_name_prefix = "draftwizard_"
class Account < ActiveRecord::Base
end
a = Account.find(1)
#=> executes SQL equal to "Select * from draftwizard_accounts where id = 1"
This attribute is very handy when you are working within a shared database that has
tables for a number of virtual schema. Prefixing all your tables with their project names makes
them easier to manage within your database management system (DBMS); by using this
attribute, you can quickly and easily utilize the Active Record library with your schema as well.


Pages:
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378