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

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Matt G. (Guest)
on 2005-12-30 20: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?

    def self.with
      old_connection = ActiveRecord::Base.connection
      ActiveRecord::Base.active_connections[ActiveRecord::Base] =
      ActiveRecord::Base.active_connections[ActiveRecord::Base] =
old_connection if 	old_connection
This topic is locked and can not be replied to.