Trying to do something with ActiveRecord :: Base.connection_pool.with_connection

So I know how it works, how it

ActiveRecord::Base.connection_pool.with_connection do |conn|
   conn.execute(sql)
end

but I'm trying to use a connection with actual Activerecord models, so something like

conn.Url.first

Is there a way to do something like this?

+3
source share
1 answer

This was found to be impossible, but in the with_connection block, any ActiveRecord calls should use the connection that is issued from the Rails connection pool

therefore in this example

ActiveRecord::Base.connection_pool.with_connection do |conn|
    Url.first
end

It should check the connection from the pool allocated for Rails in your database.yml: pool file, let your active write call use it, and then check it back in

rails 3 +... ,

Rails 2.3 ( ) http://apidock.com/rails/v2.3.8/ActiveRecord/ConnectionAdapters/ConnectionPool/with_connection

Rails 3 http://apidock.com/rails/v3.0.0/ActiveRecord/ConnectionAdapters/ConnectionPool/with_connection

http://coderrr.wordpress.com/2009/05/05/activerecords-with_connection-is-now-useful/

+10

All Articles