# File activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb, line 87 def connection self.class.connection end
The MySQL database adapter extends this and allows you to call
to get the current databases name. Useful when you are actively changing the database you are connected to and sometimes need to check the current one.
It’s possible to execute raw SQL over the currently established connection for a model.
You may configure Rails to use different databases for different models. To make sure you are querying the correct database you may do the following:
MyModel.connection.execute("UPDATE `my_models` SET `beer`='free' WHERE 1")
connection = ActiveRecord::Base.connection threads = (1..100).map do Thread.new do begin 10.times do connection.execute("SELECT SLEEP(1)") # WRONG ActiveRecord::Base.connection.execute("SELECT SLEEP(1)") # CORRECT end puts "success" rescue => e puts e.message end end end threads.each(&:join)