# New Rails app/old database - how to make them work together?

**URL:** <https://www.ruby-forum.com/t/new-rails-app-old-database-how-to-make-them-work-together/125375>\
**Category:** Rails\
**Created:** [January 8, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/new-rails-app-old-database-how-to-make-them-work-together/125375 "2008-01-08T18:02:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbandes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@cbandes](https://www.ruby-forum.com/u/cbandes)\
**Post date:** [January 8, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/new-rails-app-old-database-how-to-make-them-work-together/125375/1 "2008-01-08T18:02:00Z")

</div>

Hi guys - I have been searching and searching but I am just so confused.  
Here is my situation:

I am working on a front-end to an existing database. It’s strictly  
lookup, no create/update/drop - just trying to get data out in a pretty  
way. I was hoping to do this in RoR as a learning exercise, and as a  
first step towards moving our web-apps to a more standard platform  
(right now they are a jumble of php, javascript, etc)

Here’s my big problem:

Because of the way our db is structured, I can’t make a read-only user  
account (this is stupid, I know, but unfortunately our dbadmin won’t  
budge) - so I am concerned that if I make a migration, I might end up  
changing the db structure and affecting other users. I need a way to  
access the database that won’t allow RoR to make any db changes at all -  
just the R part of crud…

Here’s my smaller problem:

I want to acccess several databases at once - some of my data is on a  
MSSQL server, some on a MySQL server…

Am I barking up the wrong tree with RoR? This was easy (but MESSY) to do  
in php, but I’d really like to be using RoR as it seems like the way  
things are moving…

---

<div class="post-metadata">

**Author:** ![cbandes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@cbandes](https://www.ruby-forum.com/u/cbandes)\
**Post date:** [January 8, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/new-rails-app-old-database-how-to-make-them-work-together/125375/2 "2008-01-08T19:17:43Z")

</div>

You should be able to make a SQL server “View” that is read only.  
and Mysql also supports views  
[http://www.databasejournal.com/features/mysql/article.php/3399581](http://www.databasejournal.com/features/mysql/article.php/3399581)

To talk to two database in ROR go here

[http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases](http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases)

good luck

---

<div class="post-metadata">

**Author:** ![cbandes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@cbandes](https://www.ruby-forum.com/u/cbandes)\
**Post date:** [January 8, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/new-rails-app-old-database-how-to-make-them-work-together/125375/3 "2008-01-08T19:45:55Z")

</div>

Just generate models without migrations

Let’s say you have two tables like this:

widget  
widget\_id (int)  
widget\_name(varchar(50)  
category\_id

category  
category\_id (int)  
category\_name(varchar(50)

Generate models in rails:

ruby script/generate model Widget–skip-migration  
ruby script/generate model Category --skip-migration

Open up /app/models/widget.rb and make it look like this:

class Widget \< ActiveRecord::Base  
set\_table\_name “widget”  
set\_primary\_key “widget\_id”  
belongs\_to :category

end

Now open up /app/models/category.rb and make it look like this:

class Widget \< ActiveRecord::Base  
set\_table\_name “category”  
set\_primary\_key “category\_id”  
has\_many :widgets

end

That should take care of the two biggest issues - id and table names.  
Other  
things to watch out for are field names. If you have field names with  
spaces, dashes, or other characters, life gets much more difficult.

Connecting to multiple databases can be done many ways. You have to  
decide  
how that’s going to work. Another poster already gave the wiki link,  
but  
here it is again:  
[http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases](http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases)

---

<div class="post-metadata">

**Author:** ![cbandes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@cbandes](https://www.ruby-forum.com/u/cbandes)\
**Post date:** [January 8, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/new-rails-app-old-database-how-to-make-them-work-together/125375/4 "2008-01-08T20:19:12Z")

</div>

Brian H. wrote:

> Just generate models without migrations

That looks like exactly what I want to do! Thank you 🙂

Maybe I was looking in the wrong place, but none of my books/tutorials  
mentioned that this was a possibility.
