Nathan Esquenazi
2007-Dec-10 08:53 UTC
Connecting to multiple databases in rails in one application
I need to be able to have a single rails application that has models from two different databases.Some models are in database 1 locally and some are in database 2 in a remote server. I need to be able to have models, migrations, fixtures and tests for all models and controllers connected to the two different databases. I am using Rails 2 and I really have a need for this to work. Please point me in the direction of a plugin that will suit my needs. This seems like something that should be allowed in rails by now with a simple call: def MyModel :database_name => ''second_database'' end but nothing like this exists to my knowledge. Please help me. There has got to be a solution. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dusty
2007-Dec-10 15:37 UTC
Re: Connecting to multiple databases in rails in one application
The command you are looking for is establish_connection. You might want to look at the rails rdoc. Search for "Connection to multiple databases in different models" in http://api.rubyonrails.com/classes/ActiveRecord/Base.html. eg: class SomeModelDB1 < ActiveRecord::Base self.establish_connection("db1") end class SomeModelDB2 < ActiveRecord::Base self.establish_connection("db2") end database.yml db1: adapter: mysql database: somedb username: user password: pass host: localhost socket: /tmp/mysql.sock db2: adapter: mysql database: somedb2 username: user password: pass host: remotehost On Dec 10, 3:53 am, Nathan Esquenazi <rails-mailing-l...@andreas- s.net> wrote:> I need to be able to have a single rails application that has models > from two different databases.Some models are in database 1 locally and > some are in database 2 in a remote server. I need to be able to have > models, migrations, fixtures and tests for all models and controllers > connected to the two different databases. I am using Rails 2 and I > really have a need for this to work. > > Please point me in the direction of a plugin that will suit my needs. > This seems like something that should be allowed in rails by now with a > simple call: > > def MyModel > :database_name => ''second_database'' > end > > but nothing like this exists to my knowledge. Please help me. There has > got to be a solution. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nathan Esquenazi
2007-Dec-11 02:12 UTC
Re: Connecting to multiple databases in rails in one applica
And this will allow me to have fixtures and tests for models that use a different database? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---