Changing the current connection for Active Record


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] = connection yield ensure ActiveRecord::Base.active_connections[ActiveRecord::Base] = old_connection if old_connection end