Rails基于用户输入的动态数据库连接? [英] Rails Dynamic Database connection based on user input?
问题描述
我想做的是使用rails根据用户在网页上动态连接到数据库,我是ruby的新用户,不知道如何做,我想支持任何常见的数据库但是用活动记录来做它需要你有实际的数据库,这是不好的,我想能够连接到任何数据库,而实际上没有在我的服务器上。
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.
任何想法?
干杯,
Andrei
Cheers, Andrei
推荐答案
p>您可以在流程中的某个时刻通过调用 establish_connection
来更改要使用的数据库连接
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'
)
你甚至应该能够在过滤器之前做这个,如果这是你需要做的,或者调用 establish_connection
,以便他们使用不同的数据库。
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.
这篇关于Rails基于用户输入的动态数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!