Forum: Ruby on Rails More that one db

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
775b1c27ba2dcbd4c26438b8a557f0a2?d=identicon&s=25 Bahadır Doğan (xyzt)
on 2007-07-29 16:38
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?
847c9da8d177dd26f815a53c7742b7be?d=identicon&s=25 Aurélien Bottazini (Guest)
on 2007-07-29 23:49
(Received via mailing list)
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/
775b1c27ba2dcbd4c26438b8a557f0a2?d=identicon&s=25 Bahadır Doğan (xyzt)
on 2007-07-30 00:18
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/
B84d42a3a5c343f8fc6ab7d7f47fd3f5?d=identicon&s=25 Robby Russell (Guest)
on 2007-07-30 00:31
(Received via mailing list)
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 Russell
http://www.robbyonrails.com/
http://www.planetargon.com/
775b1c27ba2dcbd4c26438b8a557f0a2?d=identicon&s=25 Bahadır Doğan (xyzt)
on 2007-07-30 00:39
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 Russell 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 Russell
> http://www.robbyonrails.com/
> http://www.planetargon.com/
B84d42a3a5c343f8fc6ab7d7f47fd3f5?d=identicon&s=25 Robby Russell (Guest)
on 2007-07-30 00:46
(Received via mailing list)
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. ;-)

Robby


--
Robby Russell
http://www.robbyonrails.com/
http://www.planetargon.com/
Dfd6f9989147955682701da8227df8b4?d=identicon&s=25 Michael Wang (Guest)
on 2007-07-30 00:46
(Received via mailing list)
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 Wang
775b1c27ba2dcbd4c26438b8a557f0a2?d=identicon&s=25 Bahadır Doğan (xyzt)
on 2007-07-30 00:50
Yes, that's exactly what i need.

Thanks, Michael.
5233478c51a92b6a1a5c970cbf3a42f3?d=identicon&s=25 Isak Hansen (Guest)
on 2007-07-30 15:09
(Received via mailing list)
On 7/30/07, Bahadır Doğan <rails-mailing-list@andreas-s.net> 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
775b1c27ba2dcbd4c26438b8a557f0a2?d=identicon&s=25 Bahadır Doğan (xyzt)
on 2007-07-30 15:58


what does it mean "modifying multiple databases at once" ?



Isak Hansen wrote:
> On 7/30/07, Bahadır Doğan <rails-mailing-list@andreas-s.net> 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
5233478c51a92b6a1a5c970cbf3a42f3?d=identicon&s=25 Isak Hansen (Guest)
on 2007-07-30 16:46
(Received via mailing list)
On 7/30/07, Bahadır Doğan <rails-mailing-list@andreas-s.net> 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
This topic is locked and can not be replied to.