# Connect Rails to an existant database

**URL:** <https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699>\
**Category:** Rails\
**Created:** [July 10, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699 "2008-07-10T13:45:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zzzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zzzer](https://www.ruby-forum.com/u/zzzer)\
**Post date:** [July 10, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699/1 "2008-07-10T13:45:22Z")

</div>

Hello,

I’ve been using rails for a few days and I know how to create a new  
application and new database.

My problem is that I now have to connect Rails to an existant  
database, and I don’t know how to do …

Can somebody help me please ?

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![zzzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zzzer](https://www.ruby-forum.com/u/zzzer)\
**Post date:** [July 10, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699/2 "2008-07-10T14:36:54Z")

</div>

I’m nowhere near positive, but you might want to give this a try.  
Go into your app’s config folder and find the database.yml file. Open  
it up with SciTE. You should see:

development:  
adapter: (mysql or something)  
database: (db name)  
username:  
password:  
host:

# Warning: The database defined as ‘test’ will be erased and

# re-generated from your development database when you run ‘rake’.

# Do not set this db to the same as development or production.

test:  
adapter:  
database:  
username:  
password:

production:  
adapter:  
database:  
username:  
password:

Fill in the development area with the info to the existing database.  
That… should be it I think.

---

<div class="post-metadata">

**Author:** ![zzzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zzzer](https://www.ruby-forum.com/u/zzzer)\
**Post date:** [July 10, 2008, 2:41pm UTC](https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699/3 "2008-07-10T14:41:01Z")

</div>

> Fill in the development area with the info to the existing database.  
> That… should be it I think.

Yes, that’s what I’ve done :

development:  
host: 127.0.0.1  
adapter: postgresql  
database: RAMU\_florian  
username: florian  
password: florian

But it needs some views rhtml pages to work…  
This pages are normally generated by script/generate scaffold, but  
when I try to generate a scaffold, the table already exists on the  
database so there’s an error…

---

<div class="post-metadata">

**Author:** ![zzzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zzzer](https://www.ruby-forum.com/u/zzzer)\
**Post date:** [July 10, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699/4 "2008-07-10T14:43:22Z")

</div>

Connecting to an existing database is failrly straight forward. Open  
the database.yml file and fill in your database parameters in the  
development section.

adapter should be ‘mysql’ or ‘postgresql’ etc (depending on the  
database you use)  
database should be the name of the database you want to connect to.  
also give the username and password.

## you are done with the connection part.

To actually use the data fro existing tables, you can create models  
with appropriate name (if table name is users, you should have a Model  
with name User)

OR

create a model and use the “set\_table\_name”

Eg:

class Foo \< ActiveRecord::Base  
set\_table\_name “project”  
end

---

<div class="post-metadata">

**Author:** ![zzzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zzzer](https://www.ruby-forum.com/u/zzzer)\
**Post date:** [July 10, 2008, 2:58pm UTC](https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699/5 "2008-07-10T14:58:01Z")

</div>

> To actually use the data fro existing tables, you can create a model and use the “set\_table\_name”
> 
> Eg:
> 
> class Foo \< ActiveRecord::Base  
> set\_table\_name “project”  
> end

I’ve followed what you’ve said, but it doesn’t work :s  
When I try to go on [http://127.0.0.1:3000/correspondant](http://127.0.0.1:3000/correspondant) (after having  
put ‘set\_table\_name “correspondant”’ in correspondant.rb), this error  
appears :

Unknown action  
No action responded to index

I think it does because ‘views’ RHTML pages aren’t generated…  
No ?

---

<div class="post-metadata">

**Author:** ![zzzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zzzer](https://www.ruby-forum.com/u/zzzer)\
**Post date:** [July 10, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699/6 "2008-07-10T15:14:31Z")

</div>

not exactly. this error arises because you dont have an “index” method  
defined in the Correspondant controller. you need to define this  
method, as well as write an index.rhtml for the same, in the views/  
correspondant.

---

<div class="post-metadata">

**Author:** ![zzzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zzzer](https://www.ruby-forum.com/u/zzzer)\
**Post date:** [July 10, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699/7 "2008-07-10T15:41:28Z")

</div>

> not exactly. this error arises because you dont have an “index” method  
> defined in the Correspondant controller. you need to define this  
> method, as well as write an index.rhtml for the same, in the views/  
> correspondant.

So, I’ve copied the content of C:\Documents and Settings\Administrateur  
\ramu\app\controllers\correspondant\_controller.rb from a previous file  
and did the same for index.rhtml, but it doesn’t work…  
The error is :

Expected C:/Documents and Settings/Administrateur/ramu/app/controllers/  
correspondant\_controller.rb to define CorrespondantController

Whereas this file exists !

Here is its content :

class CorrespondantsController \< ApplicationController

# GET /correspondants

# GET /correspondants.xml

def index  
@correspondants = Correspondant.find(:all)

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @correspondants }
end

```

end

# GET /correspondants/1

# GET /correspondants/1.xml

def show  
@correspondant = Correspondant.find(params[:id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @correspondant }
end

```

end

# GET /correspondants/new

# GET /correspondants/new.xml

def new  
@correspondant = Correspondant.new

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @correspondant }
end

```

end

# GET /correspondants/1/edit

def edit  
@correspondant = Correspondant.find(params[:id])  
end

# POST /correspondants

# POST /correspondants.xml

def create  
@correspondant = Correspondant.new(params[:correspondant])

```
respond_to do |format|
  if @correspondant.save
    flash[:notice] = 'Correspondant was successfully created.'
    format.html { redirect_to(@correspondant) }
    format.xml { render :xml => @correspondant, :status

```

=\> :created, :location =\> @correspondant }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @correspondant.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

# PUT /correspondants/1

# PUT /correspondants/1.xml

def update  
@correspondant = Correspondant.find(params[:id])

```
respond_to do |format|
  if @correspondant.update_attributes(params[:correspondant])
    flash[:notice] = 'Correspondant was successfully updated.'
    format.html { redirect_to(@correspondant) }
    format.xml { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml { render :xml => @correspondant.errors, :status

```

=\> :unprocessable\_entity }  
end  
end  
end

# DELETE /correspondants/1

# DELETE /correspondants/1.xml

def destroy  
@correspondant = Correspondant.find(params[:id])  
@correspondant.destroy

```
respond_to do |format|
  format.html { redirect_to(correspondants_url) }
  format.xml { head :ok }
end

```

end  
end

I’m becoming crazy =(

---

<div class="post-metadata">

**Author:** ![zzzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zzzer](https://www.ruby-forum.com/u/zzzer)\
**Post date:** [July 10, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699/8 "2008-07-10T18:32:30Z")

</div>

your class needs to be named CorrespondantController and not  
Correspondant’s’Controller

---

<div class="post-metadata">

**Author:** ![zzzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zzzer](https://www.ruby-forum.com/u/zzzer)\
**Post date:** [July 11, 2008, 9:36am UTC](https://www.ruby-forum.com/t/connect-rails-to-an-existant-database/142699/9 "2008-07-11T09:36:38Z")

</div>

> your class needs to be named CorrespondantController and not  
> Correspondant’s’Controller

This means I have to modify what files ?
