# Unless connected? + multiple databases

**URL:** <https://www.ruby-forum.com/t/unless-connected-multiple-databases/54558>\
**Category:** Rails\
**Created:** [March 8, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/unless-connected-multiple-databases/54558 "2006-03-08T16:01:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [March 8, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/unless-connected-multiple-databases/54558/1 "2006-03-08T16:01:41Z")

</div>

Hello all.

I am still having fun with multiple databases. In this case it is the  
unless connected? statement.

ActiveRecord::Base establishes a connection that is inherited by the  
majority of my models. However one Model (Componentlog) that uses a  
separate DB is coded as:

unless Componentlog.connected?  
establish\_connection(  
:adapter =\> “oci”,  
:database =\> “”,  
:host =\> “oracle7”,  
:username =\> “vcgeneral”,  
:password =\> “manhattan”  
)  
end

I use unless connected? to stop it using a new connection every time the  
model is acccessed.

This works fine as long as that model is the first accessed by the  
application. Because ActiveRecord::Base hasn’t established it’s  
connection yet (From the looks of it) this Model happily establishes its  
own connection.

If, however, ActiveRecord::Base HAS established a connection this class  
will use that (incorrect) one because I assume it inherits from  
ActiveRecord::Base and the connected? method checks all superclasses

What I need to say essentially is that When it comes to the connection  
this class doesn’t inherit the connections.

Does anyone know a way to do this?

I have tried having an intermediate class that this one inherits from  
where the establish connection is over-ridden but if connected? doesn’t  
see a connection there is still tracks up to ActiveRecord::Base instead  
of saying “Ok, it isn’t in this class, I’ll establish a new connection”

This is all what I think is happening. I am a beginner so if I am  
missing something obvious I do apologise 🙂

Now that I think about it. I will try removing the unless connected? bit  
because I am now running mongrel with allow\_concurrency off in  
environment.rb.

Jeff

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [March 8, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/unless-connected-multiple-databases/54558/2 "2006-03-08T17:24:56Z")

</div>

> I have tried having an intermediate class that this one inherits from  
> where the establish connection is over-ridden but if connected? doesn’t  
> see a connection there is still tracks up to ActiveRecord::Base instead  
> of saying “Ok, it isn’t in this class, I’ll establish a new connection”

Having looked at the connected? method it is plain that it uses the  
active\_connections method to check the connection for a given class. So  
by calling active\_connections directly using  
ActiveRecord::Base.active\_connections[self.name]? it will only check for  
this class and not the super classes.

I have always thought that the built in RoR modules etc would be  
stupidly complex and beyond my ken (having done c++ at uni and  
experiencing that feeling) but I am glad that this understandable  
Framework is different 🙂

Jeff

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [March 8, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/unless-connected-multiple-databases/54558/3 "2006-03-08T18:34:39Z")

</div>

Hi,

I’m using also two db with rails, basically what I did  
is create two sub-class (correspond to the two db)  
from activeRecord::base. And I never use  
etablish\_connection on the avtiveRecord::bas, but only  
connect to the db via the two sub classes, all the  
tables are sub-classes of the two main sub (depend on  
which data base it should be connected) When I know I  
have to swtich database, I only have to tell rails by  
using etablish\_connection on the main sub-classes. It  
works perfectly for me. I never have problem with the  
connection.

Rails keeps the connections inside an internal hash.  
when a connection is asked for the first time, raisl  
will connect to the db and keep this connection, So  
there are always as many db as conneciton open, and It  
gives the right one when it is necessary (if you ask).  
So you don’t have “remove” a connection… rails do  
it for you.

hope it helps

Saiho

— Jeff J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (Componentlog) that uses a  
> end  
> happily establishes its  
> to the connection  
> of saying "Ok, it isn’t in this class, I’ll  
> environment.rb.  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

The mind is its own place, and in itself.  
Can make a Heaven of Hell, a Hell of Heaven.

> **[Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](https://www.yahoo.com)**
>
> Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[\[object](https://mail.yahoo.com/)**
>
> \[object

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [March 9, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/unless-connected-multiple-databases/54558/4 "2006-03-09T21:53:46Z")

</div>

As an aside, there seems to be a ActiveRecord inconsistency in that  
remove\_connection() doesnâ??t actually close down the connection, so  
instead of a

```
X.establish_connection()
...
X.remove_connection()

```

pattern, where X is the table, try using something like

```
unless X.connected? X.establish_connection()
...
X.remove_connection() # for good form

```

I noticed this while having my own ActiveRecord derived classes  
referencing different tables in the same db.

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [March 9, 2006, 10:23am UTC](https://www.ruby-forum.com/t/unless-connected-multiple-databases/54558/5 "2006-03-09T10:23:06Z")

</div>

Saiho sayoyo wrote:

> Hi,
> 
> I’m using also two db with rails, basically what I did  
> is create two sub-class (correspond to the two db)  
> from activeRecord::base. And I never use  
> etablish\_connection on the avtiveRecord::bas, but only  
> connect to the db via the two sub classes, all the  
> tables are sub-classes of the two main sub (depend on  
> which data base it should be connected) When I know I  
> have to swtich database, I only have to tell rails by  
> using etablish\_connection on the main sub-classes. It  
> works perfectly for me. I never have problem with the  
> connection.

This approach didn’t work for me because ActiveRecord::Base  
always established a connection based on the database.yaml.  
If I didn’t include anything in the YAML file then the webrick  
would error out on startup. How did you stop a Base connection  
automatically  
being created on startup?

The method I have used appears to be working but it isn’t pretty.
