lamrin
lamrin

Reputation: 1441

Migration to create different databases structures in Rails

I have 3 different organizations types and 3 different databases structures: one for each type. When org1 registers I'm creating a new database instance for org1, and when org2 registers I'm creating a new db instance, and same for org3.

Now, how do I run migrations for three different db:structure from my application soon after I create a new database instance for organizations.

I am creating a db instance from application by:

ActiveRecord::Base.connection.execute(sql)

How can I do this? Is there another approach I should be using?

Upvotes: 0

Views: 213

Answers (1)

digitalfrost
digitalfrost

Reputation: 229

The command you are looking for is establish_connection. You might want to look at the rails rdoc. Search for "Connection to multiple databases in different models" in http://api.rubyonrails.com/classes/ActiveRecord/Base.html.

Connections are usually created through ActiveRecord::Base.establish_connection and retrieved by ActiveRecord::Base.connection. All classes inheriting from ActiveRecord::Base will use this connection. But you can also set a class-specific connection. For example, if Course is an ActiveRecord::Base, but resides in a different database, you can just say Course.establish_connection and Course and all of its subclasses will use this connection instead.

Upvotes: 1

Related Questions