0

What I am trying to do is to connect to a database dynamically with rails based on what the user puts in on the web page, I am new to ruby and not sure how to do this, I want support for any common databases but doing it with active record requires you to have the actual database and that is not good, I want to be able to connect to any database without actually having it on my server.

Any ideas?

Cheers, Andrei

1 Answer 1

1

You can change the database connection that you want to use by calling establish_connection at some point during the flow

ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:host => 'localhost',
:username => 'name',
:password => 'password',
:database => 'db_name'
)

You should even be able to do this in a before filter, if that is what you need to do, or call establish_connection on individual models so that they use a different database.

Sign up to request clarification or add additional context in comments.

4 Comments

I know that but I want to be able to connect to 2 databases at the same time to map data from 1 to the other and the databases are not on localhost and do not have all the database clients I want to connect to so I can install all the gems
I need connectors that do not depend on the db clients being installed so for remote stuff
That doesn't make any sense - to access the database you have to have a connector to access the database. The database doesn't have to be on localhost, but it does have to be open for a connector to connect to. Rails code can only access a database through a connector.
I know but installing the database drivers in rails requires a database client to build the drivers and I am just wondering if there is a way around that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.