Forum: Ruby Multiple database connections

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.
Ae29a556382060cd2d41d7feee81fd74?d=identicon&s=25 James Lopes (jlopes151)
on 2009-05-01 17:11
A quick question:

Say my project needs to connect to a second database, Users stored in
one db and data in another, How would I configure my projects
database.yml?

Thanks for the help.
9b3d14efbedc70a652b441b2ec6b871b?d=identicon&s=25 Loga Ganesan (loganathan1)
on 2009-05-02 06:24
James Lopes wrote:
>
> A quick question:
>
> Say my project needs to connect to a second database, Users stored in
> one db and data in another, How would I configure my projects
> database.yml?
>
> Thanks for the help.

Hi ,
      You can put one more database entry in either the development,
test or production.
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2009-05-03 03:11
(Received via mailing list)
On May 2, 12:24 am, Loga Ganesan <loganathan_...@yahoo.co.in> wrote:
> Hi ,
>       You can put one more database entry in either the development,
> test or production.

First, this is a Rails question, so it is best asked on the Rails
list.
Secondly, the above advice is incorrect, which I guess proves the
first point. Each entry in database.yml is a connection. You can
create additional connections. See
http://pullmonkey.com/2008/4/21/ruby-on-rails-mult...

-- Mark.
990bf71a4e84e1145a3131f35656dc18?d=identicon&s=25 List Rb (listx300108u79872)
on 2009-05-03 05:43
(Received via mailing list)
I used a multi-magic (think that was the spelling) gem a while back,
worked
well as a standalone & w/ rails.
Good luck
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-05-03 21:44
(Received via mailing list)
On Sat, May 2, 2009 at 9:10 PM, Mark Thomas <mark@thomaszone.com> wrote:
>>
>> Hi ,
>>       You can put one more database entry in either the development,
>> test or production.
>
> First, this is a Rails question, so it is best asked on the Rails
> list.
> Secondly, the above advice is incorrect, which I guess proves the
> first point. Each entry in database.yml is a connection. You can
> create additional connections. See
> http://pullmonkey.com/2008/4/21/ruby-on-rails-mult...

One thing which the link doesn't seem to mention is that if you have
more than one model which needs to be in second database, you can make
an abstract superclass.

e.g.

class SecondDatabaseBase < ActiveRecord::Base
   self.abstract_class = true
   establish_connection ("connection_key")
end

where "connection_key" would be substituted for a key in database.yml,
possibly computed using RAILS_ENV.

Then just make models which are in the second database subclasses of
SecondDatabaseBase instead of ActiveRecord::Base.

Setting the class attribute abstract_class to true tells AR that the
class doesn't have any instances, but is just providing configuration
info, methods etc. to any subclasses.


--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
This topic is locked and can not be replied to.