Forum: Ruby on Rails Another Database Connectivity

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.
071e291a1589fe24f339c2cedca829df?d=identicon&s=25 Saurabh Peshkar (saurabh)
on 2009-04-06 10:51
Hi All,

I'm trying to connect another database using rails

I made changes in database.yml file

I succeed in that but with same table name,

I'm having two issues regarding that:

1) How to connect the specific table of another database (suppose table
name is Arthur and it is in database2, I'm working in database1)

2) How to connect database without making changes in database.yml file.


Any feedback appreciated,
Thanks
Saurabh
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 David Knorr (Guest)
on 2009-04-06 16:19
(Received via mailing list)
On 6 Apr., 10:51, Saurabh Peshkar <rails-mailing-l...@andreas-s.net>
wrote:
> 1) How to connect the specific table of another database (suppose table
> name is Arthur and it is in database2, I'm working in database1)
>
> 2) How to connect database without making changes in database.yml file.
>
> Any feedback appreciated,
> Thanks
> Saurabh

Given that you have a single model (Person) that you want to retrieve
data from a table in another database, you'ld make an initializer that
you could call people_connection.rb and put something like this
inside:

Person.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :username => "someuser",
  :password => "secret",
  :database => "somedatabase"
)

This is covered in the documentation for ActiveRecord::Base:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html See the
"Connection to multiple databases in different models" section.
Remember, Google is your friend, and always check the manual.

--
Best regards,
David Knorr
http://twitter.com/rubyguy
This topic is locked and can not be replied to.