Activerecord 3.1.0 Multiple Databases

ActiveRecord 3.1.0 multiple databases

I ran into the same issue yesterday while upgrading to ActiveRecord 3.1.0. I can't speak to whether there is a new recommended way of handling multiple database connections in ActiveRecord 3.1, but I did find a way to unblock myself.

It appears a connection must now be established on ActiveRecord::Base in order for it to determine the table name lengths/rules of the adapter. Along with the rest of my connections established in my database initializer, I now also have an ActiveRecord::Base connection established to one of my DBs (it doesn't matter which one).

I'd like to think there's a better solution to be found, but I'm happy to be unblocked for now.

How to access multiple databases in rails 3.1.0 app?

You should be able to use ActiveRecord::Base.establish_connection to dynamically connect to any database you like within a certain context. How you implement this is entirely application-specific, but Rails does have this capability.

Here's a very contrived example:

class User
def books
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => self.username,
:password => self.password,
:database => self.database
)

Book.all
rescue Exception => e
# ...
end
end

You would want to do actual error handling and probably establish the connection somewhere outside of an instance method, but that's up to you to decide.

Trying to use multiple databases - getting ActiveRecord Connection Not Established

The workaround is to establish a connection on ActiveRecord::Base itself. It doesn't matter to which database the connection is made. This connection is only needed so that ActiveRecord can get the meta information necessary to handle associations and whatnot. You need not use the connection at all, once established.

This issue is a duplicate of ActiveRecord 3.1.0 multiple databases and the workaround is first described here: https://stackoverflow.com/a/7406374/29729

ActiveRecord::Base doesn't belong in a hierarchy descending from ActiveRecord when connecting to two sqlserver databases

So this was a weird problem. I had to migrate connection logic into the base classes. For some reason the second call to establish_connection was making AR4 "forget" the first establish_connection.
this is weird since this was working on AR3. my models now look like this

First Base Class

class MyBase > ActiveRecord::Base
this.abstract_class = true
begin
# try the rails ways first
establish_connection(:database1_name)
rescue
# this is probably a ruby app (non-rails)
config_file = nil

# do we have a database file?
if File.exist?('config/database.yml')
config_file = 'config/database.yml'
end
# return if we could not find a config_file
return if config_file.nil?

dbconfig = YAML::load(File.open(config_file))
establish_connection(dbconfig[:database1_name])
end
end

class Order > MyBase
end

Second Base Class

class MyBase2 > ActiveRecord::Base
this.abstract_class = true
begin
# try the rails ways first
establish_connection(:database2_name)
rescue
# this is probably a ruby app (non-rails)
config_file = nil

# do we have a database file?
if File.exist?('config/database.yml')
config_file = 'config/database.yml'
end
# return if we could not find a config_file
return if config_file.nil?

dbconfig = YAML::load(File.open(config_file))
establish_connection(dbconfig[:database2_name])
end
end
class Order2 > MyBase2
end

ActiveRecord::Base doesn't belong in a hierarchy descending from ActiveRecord when connecting to two sqlserver databases

So this was a weird problem. I had to migrate connection logic into the base classes. For some reason the second call to establish_connection was making AR4 "forget" the first establish_connection.
this is weird since this was working on AR3. my models now look like this

First Base Class

class MyBase > ActiveRecord::Base
this.abstract_class = true
begin
# try the rails ways first
establish_connection(:database1_name)
rescue
# this is probably a ruby app (non-rails)
config_file = nil

# do we have a database file?
if File.exist?('config/database.yml')
config_file = 'config/database.yml'
end
# return if we could not find a config_file
return if config_file.nil?

dbconfig = YAML::load(File.open(config_file))
establish_connection(dbconfig[:database1_name])
end
end

class Order > MyBase
end

Second Base Class

class MyBase2 > ActiveRecord::Base
this.abstract_class = true
begin
# try the rails ways first
establish_connection(:database2_name)
rescue
# this is probably a ruby app (non-rails)
config_file = nil

# do we have a database file?
if File.exist?('config/database.yml')
config_file = 'config/database.yml'
end
# return if we could not find a config_file
return if config_file.nil?

dbconfig = YAML::load(File.open(config_file))
establish_connection(dbconfig[:database2_name])
end
end
class Order2 > MyBase2
end


Related Topics



Leave a reply



Submit