Forum: Ruby on Rails Database connection switching at runtime

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A58efeee01e2d1e85e12137430fa0374?d=identicon&s=25 Priya Saini (priyasaini)
on 2008-10-13 14:23
Hi All:

I want my application to authenticate user through master database and
accordingly should connect them to their specific database.

I am using the sub-domain as identifier for the user and stores
domain-database information in master database.

Im using a function responsible for mapping domains to database in
application.rb using before_filter :function_call

Default database will be master and function will identify the user
specify db to establish a connection at runtime.

The problem is ..the next time i tries to logout and use different
domain.. it still uses previous database and tries to find
domain-database related information there, which actually is in masters.

Plz suggest me some way to it.

Thanks in Advance
Priya Saini
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2008-10-13 17:38
I assume that your initial case is working:  New web session hits the
web server, application sees "new" and uses master database. User logs
in (verified against master) and is hooked up to the correct database.

Not knowing any of the details of your login/logout or session
management, it sounds like after logging out, you aren't clearing enough
data out of the session so that the same browser session would be
recognized as "new" (i.e., the same as a totally new browser session)
when you try to log back in.
A58efeee01e2d1e85e12137430fa0374?d=identicon&s=25 Priya Saini (priyasaini)
on 2008-10-14 07:11
Hi Chron:

Thanks for the reply.
Yes the initial case is working but there is something to be decided
there as well.
In application.rb
i use  before_filter :switch_db
  def switch_db
      @db = Client.find_by_domain_name(request.env['HTTP_HOST'])
      db_name =  @db.database_name
      ActiveRecord::Base.establish_connection(
      :adapter  => "mysql",
      :host     => "",
      :username => "",
      :password => "",
      :database => db_name,
      :port => "3333"
      )
  end
Initially it selects the db well but since application.rb is called
always the function gets executed each time and tries to find client in
the current db.
What condition shall be placed to by pass this function if there already
exists a database connection [other than with master].

Regards,
Priya Saini
A58efeee01e2d1e85e12137430fa0374?d=identicon&s=25 Priya Saini (priyasaini)
on 2008-10-14 07:47
I think on logout i have to end the current db connection.
How can i do this??
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2008-10-14 18:45
Not to backtrack too far, but why a separate physical database per user?

If they are all running the same application, could not a single
database be used for multiple users? You'd just need to scope the
queries by "owner", and persist the owner on record creation.

To answer your question, "disconnect!"
A58efeee01e2d1e85e12137430fa0374?d=identicon&s=25 Priya Saini (priyasaini)
on 2008-10-15 06:16
Ar Chron wrote:
> Not to backtrack too far, but why a separate physical database per user?
>
> If they are all running the same application, could not a single
> database be used for multiple users? You'd just need to scope the
> queries by "owner", and persist the owner on record creation.
>
> To answer your question, "disconnect!"

Hi Chron:
I am planning to keep seperate db for all my future clients coz keep a
single db wont be feasible since they may have their individual
requirements and configuration. Also we have Ferret Search in our
application.. so keep dbs seperate will be a better option.
Anyways i did it. I maintained a session variable to avoid function call
each time.
N thanks for "disconnect!" :)
What i am lookin for now is how to maintain two db connections
simultaneously and how to hop among them. I dont want to set up
connection each time i need it.

Thanks Again
Priya Saini
This topic is locked and can not be replied to.