More than one database per model
class Service < ActiveRecord::Base
establish_connection(
:adapter => "mysql",
:host => "myip",
:username => "myusername",
:password => "mypassword",
:database => "mydatabase"
)
end
It works
Service.all #connects to mydatabase
But I need something like that.
Service.use(mydatabase1).all #connects to mydatabase1
Service.use(mydatabase2).all #connects to mydatabase2
How can i achieve this?
Update
Database names are dynamic. I want the service model to connect to the database dynamically. When I type Service.use(weeweweaszxc).all, it should use the weewewaaszxc database.
Try a look at this question. What is the best way to handle database connections with ActiveRecord?
They define the databases in the database.yml file, as usual, and invoke this in the model:
class AnotherDatabase < ActiveRecord::Base
self.abstract_class = true
establish_connection "anotherbase_#{RAILS_ENV}"
end
Used information from Priitny answer