Dynamically Choosing a DB connection in a Model

I have a site framework that works like most CMS’s, ie. each site has
its own database, and the schema’s are identical across all the sites.

However, I need to write a reporting application, which will have only
one instance and all clients(the actual people) will connect to the
reports app to get their reports for their site(s). The point is that
the reports will have one style, and will be separate from the
production site, so if there is a problem in reports, it won’t affect
anything else, and we only have to worry about the forward facing
style/views when setting up a new site.

So I need to be able to, based on the user logged in (which is stored in
its own reports db), decide which database my reports models will
connect to to generate the reports.

In other words,

Class MyModel > ActiveRecord::Base
establish_connection(…)
end

isn’t really what I’m looking for.

Do I need to call establish_connection for every request? Ie. add a
before_filter or similar that will call establish_connection for that
model (or group of models using subclassing). Just haven’t seen any
examples where establish_connection is used outside of the models rb
file.

I am using a mongrel cluster and was just concerned I may have threading
issues or too many reconnections etc.

Found my answer:
http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs