More that one db


#1

Hello,

Sometimes I need to work with more than one databases. When I look at
database.yml file, I see that only one database is specified. Is it
possible to work with more than one databases?


#2

Hello,

Here is your answer: http://drnicwilliams.com/2007/04/12/magic-multi-
connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-
a-time/


#3

Thanks. It would be better if a built-in method for that in Rails, I
think. coz 3rd party things may not be stable…

Aurélien Bottazini wrote:

Hello,

Here is your answer: http://drnicwilliams.com/2007/04/12/magic-multi-
connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-
a-time/


#4

Bahadır Doğan wrote:

Thanks. It would be better if a built-in method for that in Rails, I
think. coz 3rd party things may not be stable…

Why? I’d guess that more than 80% of Rails applications don’t need to
ever do this.

Your argument could be used against any plugin for Rails… so is the
answer to bundle all plugins into Rails? How is that going to guarantee
anymore stability?

Connecting to multiple databases isn’t a Rails convention, so it
shouldn’t be baked into the framework. I’ve needed this in the past and
built a quick extension for ActiveRecord that provided this without any
issues and it’s been stable for over half a year.

It allows me to do the following in a model and specify when it’s
talking to which database. (save/create/destroy to one db… find/select
from another…)

class Liger < ActiveRecord::Base
handles_connection_for :other_db # maps to other_db in database.yml

end

class Tiger < ActiveRecord::Base
delegates_connection_to :liger, :on => [:save, :create, :destroy]
#…
end

Again, didn’t take much work and Rails didn’t get in my way.

Robby


Robby R.
http://www.robbyonrails.com/


#5

Bahadır Doğan wrote:

a feature in ActiveRecord to specify to which db and which table is not
a too hard thing for the core team, i think.that would solve the plural
table names problem, too. if you don’t want to specify and want to use
default, you can get them blank.

I’m very new in Rails and my approach may be wrong, i’m not sure.

Yes, an addition like that would be a very easy thing for the core team
to include. However, this opens up a can of worms. Do you know… just
how many “easy” and “small” things people want to include into the core
project? If they were all accepted, this would be increase the
complexity of the framework to maintain… and would likely slow down
their efforts.

Extend ActiveRecord and take this on for yourself. Many people have done
it (when necessary)… and we haven’t had to bother the core team in the
process. :wink:

Robby


Robby R.
http://www.robbyonrails.com/


#6

a feature in ActiveRecord to specify to which db and which table is not
a too hard thing for the core team, i think.that would solve the plural
table names problem, too. if you don’t want to specify and want to use
default, you can get them blank.

I’m very new in Rails and my approach may be wrong, i’m not sure.

Robby R. wrote:

Bahadır Doğan wrote:

Thanks. It would be better if a built-in method for that in Rails, I
think. coz 3rd party things may not be stable…

Why? I’d guess that more than 80% of Rails applications don’t need to
ever do this.

Your argument could be used against any plugin for Rails… so is the
answer to bundle all plugins into Rails? How is that going to guarantee
anymore stability?

Connecting to multiple databases isn’t a Rails convention, so it
shouldn’t be baked into the framework. I’ve needed this in the past and
built a quick extension for ActiveRecord that provided this without any
issues and it’s been stable for over half a year.

It allows me to do the following in a model and specify when it’s
talking to which database. (save/create/destroy to one db… find/select
from another…)

class Liger < ActiveRecord::Base
handles_connection_for :other_db # maps to other_db in database.yml

end

class Tiger < ActiveRecord::Base
delegates_connection_to :liger, :on => [:save, :create, :destroy]
#…
end

Again, didn’t take much work and Rails didn’t get in my way.

Robby


Robby R.
http://www.robbyonrails.com/
http://www.planetargon.com/


#7

Yes, that’s exactly what i need.

Thanks, Michael.


#8

On 7/30/07, Bahadır Doğan removed_email_address@domain.invalid wrote:

Yes, that’s exactly what i need.

Just be aware that AR (like most LAMP stuff, afaik) doesn’t support
distributed transactions. When you start modifying multiple databases
at once, data integrity is no longer guaranteed.

Isak


#9

Bahadır Doğan wrote:

Hello,

Sometimes I need to work with more than one databases. When I look at
database.yml file, I see that only one database is specified. Is it
possible to work with more than one databases?

Yes you can. In your database.yml file setup a new entry. E.g.:

legacy:
adapter: mysql
database: legacy

In your model use “establish_connection :<database_entry>”. E.g.:

class Departments < ActiveRecord::Base
establish_connection :legacy


Michael W.


#10

what does it mean “modifying multiple databases at once” ?

Isak H. wrote:

On 7/30/07, Bahadır Doğan removed_email_address@domain.invalid wrote:

Yes, that’s exactly what i need.

Just be aware that AR (like most LAMP stuff, afaik) doesn’t support
distributed transactions. When you start modifying multiple databases
at once, data integrity is no longer guaranteed.

Isak


#11

On 7/30/07, Bahadır Doğan removed_email_address@domain.invalid wrote:

what does it mean “modifying multiple databases at once” ?

Let me rephrase:
You can’t make a transaction span multiple databases using AR (or Ruby
in general, as far as i know).

If that’s a requirement, your best bet is probably handing the work
over to a Java web service or something along those lines.
Then again, this may not be neccessary for your application.

Isak