# Using Ruby on Rails with existing database

**URL:** <https://www.ruby-forum.com/t/using-ruby-on-rails-with-existing-database/134280>\
**Category:** Rails\
**Created:** [April 9, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/using-ruby-on-rails-with-existing-database/134280 "2008-04-09T13:31:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Ashley](https://www.ruby-forum.com/u/Ashley)\
**Post date:** [April 9, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/using-ruby-on-rails-with-existing-database/134280/1 "2008-04-09T13:31:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Ashley](https://www.ruby-forum.com/u/Ashley)\
**Post date:** [April 9, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/using-ruby-on-rails-with-existing-database/134280/2 "2008-04-09T19:21:58Z")

</div>

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](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://sl33p3r.free.fr/tutorials/rails/legacy/legacy_databases.html)  
[http://wiki.rubyonrails.org/rails/pages/howtouselegacyschemas](http://wiki.rubyonrails.org/rails/pages/howtouselegacyschemas)

Good luck. You’re going to need it. 😉

---

<div class="post-metadata">

**Author:** ![Ashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Ashley](https://www.ruby-forum.com/u/Ashley)\
**Post date:** [April 10, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/using-ruby-on-rails-with-existing-database/134280/3 "2008-04-10T15:30:13Z")

</div>

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

On Apr 9, 6:21 pm, Daniel W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Ashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Ashley](https://www.ruby-forum.com/u/Ashley)\
**Post date:** [April 14, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/using-ruby-on-rails-with-existing-database/134280/4 "2008-04-14T17:17:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Ashley](https://www.ruby-forum.com/u/Ashley)\
**Post date:** [April 15, 2008, 2:20pm UTC](https://www.ruby-forum.com/t/using-ruby-on-rails-with-existing-database/134280/5 "2008-04-15T14:20:41Z")

</div>

Maybe this is of interest:  
[http://www.elctech.com/blog/refreshing-rails-generated-views](http://www.elctech.com/blog/refreshing-rails-generated-views)

Allan

---

<div class="post-metadata">

**Author:** ![Ashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Ashley](https://www.ruby-forum.com/u/Ashley)\
**Post date:** [April 23, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/using-ruby-on-rails-with-existing-database/134280/6 "2008-04-23T14:06:50Z")

</div>

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 W.  
initially.

---

<div class="post-metadata">

**Author:** ![Ashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Ashley](https://www.ruby-forum.com/u/Ashley)\
**Post date:** [April 14, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/using-ruby-on-rails-with-existing-database/134280/7 "2008-04-14T18:03:22Z")

</div>

On Apr 14, 4:16 pm, Ashley [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone give advice on the two questions I’ve asked? Thanks again  
> to anyone that can help.
> 
> On Apr 10, 2:29 pm, Ashley [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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
