Forum: Ruby on Rails Is there any way to use separated databases for models in one RoR app?

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.
Paweł K (Guest)
on 2008-12-06 15:18
(Received via mailing list)
Like in topic - is there any simple and elegant way to use separated
databases for specified models in one RoR app?

Here is the example:
I'd like to have separated DB for user data (model "Account" with
credentials, account info etc. for security purpose), different one
for Payment model (date, amount etc.) and third one for (i.e.) store
data.

Is there any way to do this with maintaining ActiveRecord relations
between models? (like account.has_many => payments :D)???

Thanks in advance guys :)
Pawe³ Komarnicki, http://komarnicki.webd.pl
Frederick C. (Guest)
on 2008-12-06 16:32
(Received via mailing list)
On Dec 6, 1:17 pm, Pawe³ K <removed_email_address@domain.invalid> wrote:
> Like in topic - is there any simple and elegant way to use separated
> databases for specified models in one RoR app?
>
> Here is the example:
> I'd like to have separated DB for user data (model "Account" with
> credentials, account info etc. for security purpose), different one
> for Payment model (date, amount etc.) and third one for (i.e.) store
> data.
see the section titled "Connection to multiple databases in different
models" at http://rails.rubyonrails.com/classes/ActiveRecord/Base.html
>
> Is there any way to do this with maintaining ActiveRecord relations
> between models? (like account.has_many => payments :D)???
>
Probably (unless you try an do things like joins between two databases
not served by the same database server or stuff like that)

Fred
Paweł K (Guest)
on 2008-12-06 23:45
(Received via mailing list)
Thanks Fred,

indeed there is some info about that, but... it's a bit unclear to me
how to use it... Do I specify establisch_connection command in model
file? Or in configuration script?

Thanks for help :)
Pawe³ Komarnicki
Maurício L. (Guest)
on 2008-12-07 00:22
(Received via mailing list)
You specify it at your model, eg:

class Client < ActiveRecord::Base
  establish_connection *put_your_config_here*
end

Then all queries against that model will use it's connection.

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)



2008/12/6 Pawe³ K <removed_email_address@domain.invalid>:
Frederick C. (Guest)
on 2008-12-07 11:58
(Received via mailing list)
On Dec 6, 10:21 pm, "Maurício Linhares" <removed_email_address@domain.invalid>
wrote:
> You specify it at your model, eg:
>
> class Client < ActiveRecord::Base
>   establish_connection *put_your_config_here*
> end
>
> Then all queries against that model will use it's connection.

Yup. One tip if you've got several models using the same
configuratrion is that you can do

class Foo < ActiveRecord::Base
  establish_connection :other_database
end

and that will take the parameters from database.yml for the
configuration named other_database.

Fred
This topic is locked and can not be replied to.