# Selecting a database from the application itself?

**URL:** <https://www.ruby-forum.com/t/selecting-a-database-from-the-application-itself/118380>\
**Category:** Rails\
**Created:** [October 26, 2007, 11:04am UTC](https://www.ruby-forum.com/t/selecting-a-database-from-the-application-itself/118380 "2007-10-26T11:04:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 26, 2007, 11:04am UTC](https://www.ruby-forum.com/t/selecting-a-database-from-the-application-itself/118380/1 "2007-10-26T11:04:09Z")

</div>

Hi everybody,

I just started thinking about a project and am seeking some help  
regarding its design.  
The idea would be to create labels (i.e. address labels).I could let  
users type the text manually, but I dont think they would appreciate  
that when they’ll want to print 5000 addresses.  
I can also probably let them import csv files, that should not be a  
problem, but playing with those files is not extremely user friendly  
either. Besides, I dont want them to get bored after 5 minutes and  
going back to their good old excel after 5 minutes.

The most “exciting” feature i thought of is also the one that scares  
me the most when it comes to coding:  
I’d love to let users actually connect to any database they want,  
retrieve a list of tables and fields and let them select anything they  
want on the labels.  
I can already see the tables with their fields that they could drag  
and drop on a label template… Well, I’m dreaming, and I’d love to  
know if this is going to stay just a dream or if this is actually  
possible using Ruby and Rails…

Summary : Is there any way I can let users select (from the  
application) a database they want to use? Or is the database.yml file  
the only way of connecting to a database?

Any help would be greatly appreciated,

Thank you!

Francois

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 26, 2007, 11:24am UTC](https://www.ruby-forum.com/t/selecting-a-database-from-the-application-itself/118380/2 "2007-10-26T11:24:39Z")

</div>

On Oct 26, 10:03 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Summary : Is there any way I can let users select (from the  
> application) a database they want to use? Or is the database.yml file  
> the only way of connecting to a database?

Yes, it is possible but not easy in RoR.

If you can stick to a single database and follow RoR conventions then  
life is so much easier.

I arranged things so that I have one server per database, I put the  
server name in database.yml:

# This defines the common login info

login: &login  
adapter: mysql  
username: root  
password:  
host: localhost

db1:  
\<\<: \*login  
database: project\_db1  
app\_server: [host.domain.com:4101](http://host.domain.com:4101)

db2:  
\<\<: \*login  
database: project\_db2  
app\_server: [host.domain.com:4102](http://host.domain.com:4102)

and (re-)read that file with:

APP\_CONFIG = YAML::load(File.open(“#{RAILS\_ROOT}/config/  
database.yml”))

I can then use APP\_CONFIG[] in a view:

\<%= link\_to h(project.name), “http://#{APP\_CONFIG[project.name]  
[‘app\_server’]}/my\_action” %\>

It’s a bit hacky but I hope that it gives you some ideas.

Allan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 26, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/selecting-a-database-from-the-application-itself/118380/3 "2007-10-26T12:51:21Z")

</div>

On 10/26/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Summary : Is there any way I can let users select (from the  
> application) a database they want to use? Or is the database.yml file  
> the only way of connecting to a database?

Hi Francois,

Actually, it’s not too hard IMHO. You certainly don’t need a  
different URL / app instance for each database. In fact, you can pick  
a database and run the pending migrations from a before filter in just  
a few lines of code. Here’s a demo:

g@bang:~/tmpapp$ cat db/migrate/001\_create\_initial\_tables.rb  
class CreateInitialTables \< ActiveRecord::Migration  
def self.up  
create\_table :things do |t|  
t.column :name, :string  
end  
end

def self.down  
drop\_table :things  
end  
end  
g@bang:~/tmpapp$ cat app/models/thing.rb  
class Thing \< ActiveRecord::Base  
end  
g@bang:~/tmpapp$ cat app/controllers/things\_controller.rb  
require ‘ostruct’

class ThingsController \< ApplicationController  
before\_filter :connect\_to\_database  
def connect\_to\_database  
config = session[:database] ||=  
ActiveRecord::Base.configurations[RAILS\_ENV]  
ActiveRecord::Base.establish\_connection(config)  
ActiveRecord::Migrator.migrate(“db/migrate”)  
@database = OpenStruct.new(config)  
end

def set\_database  
session[:database] = params[:database]  
redirect\_to :action =\> ‘list’  
end

def list  
@things = Thing.find(:all)  
@thing = Thing.new  
end

def create  
Thing.create(params[:thing])  
redirect\_to :action =\> ‘list’  
end  
end  
g@bang:~/tmpapp$ cat app/views/things/list.rhtml

## Database
\<% form\_for :database, @database, :url =\> {:action =\> 'set\_database'} do |form| %\> 

| Adapter: | \<%= form.text\_field 'adapter' %\> |
| Database: | \<%= form.text\_field 'database' %\> |
| User: | \<%= form.text\_field 'username' %\> |
| Pass: | \<%= form.text\_field 'password' %\> |

 \<%= submit\_tag 'Switch Database' %\> \<% end %\>
* * *

## Things
 \<% @things.each do |thing| %\> \<% end %\>

| ID | Name |
| \<%= thing.id %\> | \<%= thing.name %\> |

## Create Thing
\<% form\_for :thing, @thing, :url =\> {:action =\> 'create'} do |form| %\> Name: \<%= form.text\_field 'name' %\> \<%= submit\_tag 'Create' %\> \<% end %\>

Hope that helps!

Regards,  
George.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 5, 2007, 11:42am UTC](https://www.ruby-forum.com/t/selecting-a-database-from-the-application-itself/118380/4 "2007-11-05T11:42:26Z")

</div>

Thank you very much, I will try that 🙂
