connection() public

Returns the connection currently associated with the class. This can also be used to "borrow" the connection to do database work that isn’t easily done without going straight to SQL.

Show source
Register or log in to add new notes.
August 20, 2008
1 thank

Current Database Name

The MySQL database adapter extends this and allows you to call

  ActiveRecord::Base.connection.current_database

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.

http://apidock.com/rails/ActiveRecord/ConnectionAdapters/MysqlAdapter/current_database

November 7, 2008 - (v1.0.0 - v2.1.0)
0 thanks

Using your model's connection

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")