# Ruby existing database

**URL:** <https://www.ruby-forum.com/t/ruby-existing-database/187265>\
**Category:** Rails\
**Created:** [April 20, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/ruby-existing-database/187265 "2010-04-20T19:50:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wjrogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8797f3/32.png) [@wjrogers](https://www.ruby-forum.com/u/wjrogers)\
**Post date:** [April 20, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/ruby-existing-database/187265/1 "2010-04-20T19:50:08Z")

</div>

Hello all from Ruby newbie,

I’m sure this has been asked many times, but I can’t find the answer.

I would like to use Ruby for an existing database.

It does not use id for the primary key, and the table name are not  
plural.

The tables are already loaded and filled with data.

How can I get CRUD modules for the existing tables without changing the  
database structure in any way?

---

<div class="post-metadata">

**Author:** ![wjrogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8797f3/32.png) [@wjrogers](https://www.ruby-forum.com/u/wjrogers)\
**Post date:** [April 20, 2010, 8:06pm UTC](https://www.ruby-forum.com/t/ruby-existing-database/187265/2 "2010-04-20T20:06:57Z")

</div>

On Tue, 2010-04-20 at 19:50 +0200, Will R. wrote:

> How can I get CRUD modules for the existing tables without changing the  
> database structure in any way?

* * *

inside the model class…

set\_table\_name :some\_table\_in\_your\_db

set\_primary\_key “some\_primary\_key\_other\_than\_id”

Anyone else notice that [http://api.rubyonrails.org](http://api.rubyonrails.org) has been hijacked?

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![wjrogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8797f3/32.png) [@wjrogers](https://www.ruby-forum.com/u/wjrogers)\
**Post date:** [April 21, 2010, 12:09am UTC](https://www.ruby-forum.com/t/ruby-existing-database/187265/3 "2010-04-21T00:09:34Z")

</div>

Will R. wrote:

> Hello all from Ruby newbie,
> 
> I’m sure this has been asked many times, but I can’t find the answer.
> 
> I would like to use Ruby for an existing database.
> 
> It does not use id for the primary key, and the table name are not  
> plural.
> 
> The tables are already loaded and filled with data.
> 
> How can I get CRUD modules for the existing tables without changing the  
> database structure in any way?

That depends. Are you using Rails? You specified Ruby without  
mentioning any other libraries…

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![wjrogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8797f3/32.png) [@wjrogers](https://www.ruby-forum.com/u/wjrogers)\
**Post date:** [April 21, 2010, 9:06pm UTC](https://www.ruby-forum.com/t/ruby-existing-database/187265/4 "2010-04-21T21:06:08Z")

</div>

Yes, I have installed Rails, as well as oracle\_enhanced adapter and oci8  
installed.

I found an interesting link about using legacy databases:

[Rails and Legacy Databases - RailsConf 2009](http://www.slideshare.net/napcs/rails-and-legacy-databases-railsconf-2009)

I did: rails --database=oracle ora\_t1

I took the advice of Craig W. and set the primary key and sequence as  
suggested in ctkagency.rb

ruby script\generate scaffold CTK\_AGENCY AGENCY\_SEQUENCE:number  
AGENCY\_NAME:text

I got to the localhost/3000 page ok, showing:

About your applicationâ€™s environment  
Ruby version 1.8.6 (i386-mswin32)  
RubyGems version 1.3.6  
Rack version 1.0  
Rails version 2.3.5  
Active Record version 2.3.5  
Active Resource version 2.3.5  
Action Mailer version 2.3.5  
Active Support version 2.3.5  
Application root C:/Ruby/rails\_apps/ora\_t1  
Environment development  
Database adapter oracle\_enhanced  
Database schema version 0

The next page bombed.

I think it’s because my table names have underscores, like “CTK\_AGENCY”.

Processing Rails::InfoController#properties (for 127.0.0.1 at 2010-04-21  
14:49:21) [GET]  
e[4;36;1mSQL (328.0ms)e[0m e[0;1mselect  
decode(table\_name,upper(table\_name),lower(table\_name),table\_name) name  
from all\_tables where owner = sys\_context(‘userenv’,‘session\_user’)e[0m  
Completed in 421ms (View: 0, DB: 328) | 200 OK  
[[http://localhost/rails/info/properties](http://localhost/rails/info/properties)]  
/!\ FAILSAFE /!\ Wed Apr 21 14:49:33 -0400 2010  
Status: 500 Internal Server Error  
Expected C:/Ruby/rails\_apps/ora\_t1/app/helpers/ctkagen\_cies\_helper.rb  
to define CtkagenCiesHelper  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active\_support/dependencies.rb:426:in  
`load\_missing\_constant’  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active\_support/dependencies.rb:80:in  
`const\_missing’  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active\_support/dependencies.rb:92:in  
`const\_missing’  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active\_support/inflector.rb:361:in  
`constantize’  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active\_support/inflector.rb:360:in  
`each’  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active\_support/inflector.rb:360:in  
`constantize’  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active\_support/core\_ext/string/inflections.rb:162:in  
`constantize’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action\_controller/helpers.rb:137:in  
`helper’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action\_controller/helpers.rb:115:in  
`each’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action\_controller/helpers.rb:115:in  
`helper’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action\_controller/helpers.rb:120:in  
`helper’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action\_controller/helpers.rb:115:in  
`each’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action\_controller/helpers.rb:115:in  
`helper’  
C:/Ruby/rails\_apps/ora\_t1/app/controllers/application\_controller.rb:5
