Conceptually, I had exactly the same problem. I wanted to subclass ActiveRecord :: Base and build a schema for this connection. It took me a long time to understand, and a lot of diving into ActiveRecord :: Base, Schema, and Migration, but I found a solution that works, and it is really very simple.
Under the hood, Schema is a subclass of Migration, and it calls instance_eval in the block that you provide. Therefore, we are in the scope of the Migration class and can change the @connection instance variable to connect the ActiveRecord :: Base subclass, i.e.
ActiveRecord::Schema.define do @connection = TableB.connection create_table :table_bs do |t| t.column :text, :string end end
I understand that this answer is probably too late! But it may be useful to someone.
Chris
source share