The usual trick in situations like this is to use a thread-local
variable, set in the controller action and accessed from the models.
It's a somewhat leaky abstraction, but probably the most reliable way
to do things (ActiveSupport uses it for the Time.zone stuff). The code
would look like (sketch):

in application_controller.rb (or elsewhere):

before_filter :set_customer_db

def set_customer_db
  Thread.current[:customer_db] = ...
end

I can't think of how exactly you'll end up accessing it in the models,
but the magic_multi_connections gem should provide some guidance.

--Matt Jones


On Nov 2, 8:09 am, jmamma <jackli...@gmail.com> wrote:
> Thanks Amar.  Sharding isn't exactly what I need here, but some
> interesting info nonetheless.
>
> I have a main db ( users, applications, etc ) and a database for each
> customer ( products, orders, etc )
>
> When I want to get all the products, for example, I need to connect
> to
> a particular customers version of products.  I can do it by using
> ActiveRecord::Base.establish_connection (:connection_name) in the
> controller
> but its not perfect... also there are some plugins that use some
> activeRecord
> stuff in them and the model doesn't know which db its supposed to
> connect to
> so I have to modify all those gems and put the establish_connection in
> there too.
>
> I put the customer in session on user login, and then I can easily get
> which customer
> db I need, but again that doesn't work so well in the models since the
> session isn't
> easily available in the models.
>
> On Nov 2, 7:12 am, Amar Daxini <rails-mailing-l...@andreas-s.net>
> wrote:
>
>
>
> > jmamma wrote:
> > > Hey everyone!  I have a problem that's been making me a little nuts.
> > > I would like to use RoR for a upcoming project, but if I can't get
> > > past this hiccup I'm
> > > afraid I'll have to use PHP or Java.  Ugh.
>
> > check out data_fabric 
> > gemhttp://www.codefutures.com/database-sharding/andcheck this article
> > --
> > Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to