Re: Changing a model''s database connection

Michaël
Mon, 19 Jul 2010 18:42:07 -0000

On 19 July, 19:32, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> On Jul 19, 6:08 pm, Michaël
<michael.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
>
>
>
>
> > Hi,
> > I''ve tried the following, but it doesn''t seem to
have any effect:
>
> >      def build_report:
>
> >         Customer.establish_connection(
> >             :adapter  => "mysql",
> >             :host     => "hostname",
> >             :username => "user",
> >             :password => "password",
> >             :database => "schema"
> >         )
>
> >         @customer = Customer.find(@criteria[:customer_id])
>
> >         ..... <report logic here>
>
> >      end
>
> > Is it possible to achieve what I want to do this way, or do I need to
> > look along other lines?
>
> this would only affect queries done against the customers table.
> queries to any other tables would still go to the
''normal'' database.
> Is that what''s happening?
>
> Fred

That''s actually what I want to achieve, but I''ve done the
above and it
doesn''t seem to work. I know that reports only use information from
the Customers, Products and Shop models, so I only want to change the
database connection for these three while generating the report, and
then switch it back after its done.

As far as load balancing is concerned, my ultimate goal is to push
that to the database level and have the database server handle that,
but I need to implement something quickly as an intermediate step.