views:

549

answers:

4

Complete newbie researching Rails. Can Rails be used with a read-only schema that doesn't conform to the Rails' default naming and design conventions?

For example, my database's schema has base tables that use string columns for unique primary keys. For example, a base table called Jobs, might have a unique primary key defined as Jobs.Job and the value might be 'D01234'. Now, a child entity of Jobs might be CostCodes where the unique primary key is the aggregate of CostCodes.Job (an FK reference back tothe Jobs table) and CostCodes.CostCode (a local string column).

For what it's worth... this is the stock Sage Timberline Office database.

Any pointers?

+4  A: 

You're going to want to open up the Model for your particular table and set the following:

set_table_name 'user'
set_primary_key 'user_name'

And if you are managing relationships you may want to explicitly specify the join table:

has_and_belongs_to_many :page, :join_table => 'user_page'
Robotsu
+5  A: 

Yes, it can be done.

Dave Thomas has a blog post describing a little of what's necessary: directives such as

set_table_name  "orders"
set_primary_key "o_id"

in your ActiveRecord model definitions.

Some more references:

Googling for "activerecord legacy database" ought to pick up some more.

Mike Woodhouse
I've been searching on Google quite a bit - but these search terms nailed it for me: http://wharsojo.wordpress.com/2006/06/19/ruby-on-rails-legacy-database-with-no-non-auto-increment-and-manual-update-primary-key/
ankushnarula
+1  A: 

Yes, ActiveRecord (the default ORM) will let you customize whatever you need to customize about the database you're accessing - see Mike's and Robotsu's answers for details.

You can also replace ActiveRecord with another ORM that is more flexible. DataMapper is a drop-in replacement that seems to have some traction.

Sarah Mei
A: 

The other answers already cover your question, but a great resource for all these ActiveRecord questions is Pro ActiveRecord by Apress, it's really good, you should look it up.

railsninja