Forum: Ruby on Rails Changing the current connection for Active Record

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.
B8684b9e5b2f818b8082e18b217a66a4?d=identicon&s=25 Matt Goss (javamllama)
on 2005-12-30 19:04
(Received via mailing list)
Hi Everyone,
I've just updated my Rails install to 1.0 and have noticed that the
ActiveRecord connection handling is somewhat different. I'll explain...
I've declared a block handler that will change the connection for
ActiveRecord::Base prior to yielding to the block, then restore the
original connection (see code snippet below).  I'm doing this in order
to allow for setup code in my unit tests to run as an admin user. This
way I don't have to grant unneeded privileges for my application user
just so the unit tests will run.  Does anyone understand the connection
handling and how it changed enough to help?
Thanks,
Matt

<snippet>
    def self.with
      old_connection = ActiveRecord::Base.connection
      ActiveRecord::Base.active_connections[ActiveRecord::Base] =
connection
      yield
    ensure
      ActiveRecord::Base.active_connections[ActiveRecord::Base] =
old_connection if 	old_connection
    end
</snippet>
This topic is locked and can not be replied to.