I'm attempting to dynamically create a Class and assign one of several database connections to each Class.
I'm working with anywhere between two or three databases which change over time, therefore, I'm hesitant to store each connection string in a separate Class and inherit from it instead of ActiveRecord::Base.
The following throws an error "RuntimeError: Anonymous class is not allowed.", but I'm not sure how to work around it or if there are better alternatives.
class ClassFactory
def self.create_class(new_class, table, db_connection)
c = Class.new(ActiveRecord::Base) do
db = db_connection
self.table_name = table
establish_connection(:adapter => db.database_type, :host => db.host, :database => db.database, :username => db.username, :password => db.password).connection
end
Module.const_set new_class, c
end
end