Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails, activerecord, get current connection specification

I am writing code that will migrate some data from one database to another, over-writing some data in the destination. It uses ActiveRecord, since it's associated with a Rails app using AR already.

Since some data will be over-written, I'd like to provide a confirmation prompt that tells the user the actual connection dictionary/spec used for the destination database connection. you know, adapter, host, username, password, database, the things you'd list in database.yml.

I can take the model for the stuff I'm writing to and ask for SomeModel.connection.... But there seems to be no API at all to get the actual connection spec out of a live connection object.

Really? Am I missing something? Any other ideas, even undocumented api?

like image 223
jrochkind Avatar asked Dec 29 '11 20:12

jrochkind


People also ask

What is ActiveRecord :: Base connection?

simply tells you that a connection has already been opened. It may not necessarily still be active. To check whether the connection is currently active, call ActiveRecord::Base.

Is ActiveRecord an ORM?

ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.

What does ActiveRecord base mean?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is ActiveRecord in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.


2 Answers

Similar to the way that you can call connection on a model, you can make a call to connection on ActiveRecord::Base.

ActiveRecord::Base.connection_config 

Look at the docs for ActiveRecord::Base, as there are other methods that allow you to get/set attributes about the connection.

Update

The connection_config became deprecated and will be removed from Rails 6.2. connection_db_config should be used instead:

ActiveRecord::Base.connection_db_config 
like image 111
Batkins Avatar answered Sep 18 '22 19:09

Batkins


The answer is SomeModel.connection_config

like image 37
jrochkind Avatar answered Sep 22 '22 19:09

jrochkind