Hi friends, We have JUST started with Ruby/Rails... and we are very much impressed with the scaffold generator which gets us kick-started by creating even the DB-Schema Migration scripts fer us, which is DB agnostic..!! But then, me & my friends began wondering... -- WHAT if we had ALREADY designed a DB-Schema & even made the Tables, etc.?? (or , more realistically, GIVEN an existing schema info..!) -- WHAT if this schema did NOT follow the "Conventions" that Rails NEEDS in order to infer details regarding the Models, Controllers, etc..?!? Will be grateful if anyone could give us some pointers in this regard... Cheers,
on 2008-10-23 18:02
on 2008-10-23 19:33
On Oct 23, 10:37 pm, deepFritz <firstname.lastname@example.org> wrote: > -- WHAT if we had ALREADY designed a DB-Schema & even made the Tables, > Cheers, I'd try to refactor the schema (assuming this is permissible) to fit the ActiveRecord conventions which I think are reasonable - the big thing being the synthetic id's and foreign id's and also the table names. If I can't rename the tables, I might try to use the set_table_name directive ( http://api.rubyonrails.org/classes/ActiveRecord/Ba... ). If the table was using a data field for its primary key, or a composite, i'd add in an 'id' integer field and make it the pk, and apply unique not null to the old primary key; I'd then populate id with incrementing integers. Set up a sequence or autoincrement option for this id column (I might have to check that AR is happy with this; maybe I have to tell it what sequence to use using set_sequence_name - depending on the db). Alternatively AR also has a set_primary_key; if the existing pk was already an incrementing number, I might try this instead. See same url above and look at the other api options there. For assocations, if the old pk/fk's were not based on incrementing numbers, I'd set up the foreign key in a similar way to the 'id' fields using the rails naming conventions and try to update them by using the old primary and foreign keys to basically bootstrap the new system - my sql is a little rusty, but you could probably do an UPDATE or script it if you had to. I'd probably also have to qualify the assocations made in the models using options like :class_name and :foreign_key etc- see the AR rdocs on assocations. ActiveRecord is the big issue. Your controllers can do what they like. -- Daniel B.