Using Ruby on Rails with existing database

Hi, I'm a complete newbie to this, I'm afraid. Apologies if this has been answered elsewhere, but I've had a look, and can't find the answers I'm looking for, especially for using Rails 2.0.

We have an existing database which we would need to connect Rails to, and the database structure is not what Rails would seem to expect. Firstly, tables are not always pluralised. Secondly, some primary keys are not integers. Thirdly, some primary keys are composite.

Now if I'm correct, I would need to do the following, eg. for an "ENTITY" table: script/generate scaffold entity {field definitions here} --skip- migration Is there any way to avoid having to type in all of the field definitions manually?

Then I would need to edit models/entity.rb and specify the following: class Entity < ActiveRecord::Base    set_table_name "entity"    set_primary_key "en_key" end

That should, I believe, take care of the table names not conforming to the Rails convention. What about the primary keys? Is there anything else I'm missing? Sorry to trouble you guys with all of this, but in all honesty, the documentation seems non-existent for this area - and I'm sure many people that look to use Rails have existing databases that do not conform to the conventions.

Just in case it makes any difference, the database I'm looking to use is a Firebird db.

Thanks in advance for any help you can give me. Cheers Ashley

Ashley wrote:

Firstly, tables are not always pluralised.

You can define the table name the model should look for within the model itself with set_table_name.

Secondly, some primary keys are not integers.

You poor soul. Are they numbers (even if held as strings)? If so you can access the value before it gets type casted, or cast it yourself when you need it.

Rails RDoc for that: http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001449

Thirdly, some primary keys are composite.

There is at least one plugin that handles composite keys. Google should find it for you.

Finally, here are a couple guides for working with legacy databases.

http://sl33p3r.free.fr/tutorials/rails/legacy/legacy_databases.html http://wiki.rubyonrails.org/rails/pages/howtouselegacyschemas

Good luck. You're going to need it. :wink:

Thanks very much for your reply Daniel.

Can I assume that there isn't any way to get Rails to read the table fields, that the fields have to be defined manually when calling scaffold?

Also, and seemingly more importantly, am I to assume that perhaps Rails isn't the way to go if our primary keys aren't always integers? They are often strings, and not just numbers stored as strings, eg 'AL'.

Once again, thanks for your help and for the links you've provided, they are very much appreciated.

Cheers

Ashley

Can anyone give advice on the two questions I've asked? Thanks again to anyone that can help.

Can anyone give advice on the two questions I've asked? Thanks again to anyone that can help.

> Thanks very much for your reply Daniel.

> Can I assume that there isn't any way to get Rails to read the table > fields, that the fields have to be defined manually when calling > scaffold?

I don't think there is, but maybe a bit of perl or ruby working off the outputs of a SQL 'describe table' command might do the trick. May be Dr Nic's Magic Models (Google for it) can tell you how Rails is interpreting your db without too much effort on your part.

> Also, and seemingly more importantly, am I to assume that perhaps > Rails isn't the way to go if our primary keys aren't always integers? > They are often strings, and not just numbers stored as strings, eg > 'AL'.

I'm not sure if Rails really cares too much about this, but I am speaking from a position of ignorance. Give it a go and see what happens.

Allan

Maybe this is of interest: Elctech.com is for sale | HugeDomains

Allan

Just to end the thread, Allan's top on Dr Nic's Magic Models proved very useful. As I said before, thanks a lot Allan, and to Daniel Waite initially.