From: Brian Candler Date: 2010-06-08T23:11:44+09:00 Subject: Re: Looking for ORM for 'legacy' database. Dave Howell wrote: > I actually used to have all my primary keys' names like 'product_id" in > table 'products' in anticipation of using Rails with it one day, but I > got tired of dealing with underscores, and stripped them out. Aside: Rails (or ActiveRecord) expects that the primary key is called "id". Now, I've written applications which connect ActiveRecord to an existing legacy database which doesn't respect Rails conventions, and they work fine. For example, if I want a model class called Playlist then AR will normally assume a table called "playlists" and a primary key of "id", but these are easily overridden: class Playlist < ActiveRecord::Base self.table_name = 'venue_playlist' self.primary_key = 'plt_id' # Similarly for foreign key relations has_many :playlist_items, :foreign_key => 'pli_playlist_id', :order => 'pli_order', :dependent => :delete_all end I'm not 100% sure how well AR copes with a non-numeric primary key, but I believe it's OK. Once you define the primary key explicitly, it no longer tries to auto-assign it from a sequence, as far as I remember. So you'll have to remember to set it explicitly when creating new objects/rows. Where AR really falls down is when you have a composite primary key. ISTR I came across some plugin for this, but I've never tried it. > Because > that's really at the heart of my search for an alternative to Rails. > It's not actually a pre-existing database. I can still rewrite the > schema as I see fit. I *could* start from scratch, build the database > from within Rails, then import the data from its present true purgatory > in a badly-designed Access database. However, too many of the defaults > and assumptions in Rails are ones that I'm not willing to accept. You really don't need to use migrations in Rails/AR. Trust me, I know. The DBA team I work with won't let me have schema-level access to any production database, so I have to provide them with SQL scripts which they run manually to make schema changes. Hence the app itself just reads the column definitions from the DB and builds its object accessor methods from that; it cannot and does not push out any schema modifications. So I'd say: * design and build your database how you want * connect ActiveRecord to it as above * watch it work * if you really get stuck, then look at other ORMs (but I can't vouch how well they will work with the rest of Rails, at least before Rails 3) Regards, Brian. -- Posted via http://www.ruby-forum.com/.