# ActiveRecord not as smart as I thought it would be? or is it me?

**URL:** <https://www.ruby-forum.com/t/activerecord-not-as-smart-as-i-thought-it-would-be-or-is-it-me/169199>\
**Category:** Rails\
**Created:** [May 31, 2009, 4:19pm UTC](https://www.ruby-forum.com/t/activerecord-not-as-smart-as-i-thought-it-would-be-or-is-it-me/169199 "2009-05-31T16:19:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 31, 2009, 4:19pm UTC](https://www.ruby-forum.com/t/activerecord-not-as-smart-as-i-thought-it-would-be-or-is-it-me/169199/1 "2009-05-31T16:19:04Z")

</div>

Hello,

I have been wrestling for a couple of days trying to make my  
application work with 2 different databases simultaneously. Finally  
got it running but I am a bit disappointed at what I needed to do in  
order to make it work. I have a couple case scenarios:

First case scenario:  
MySQL A: 6 tables  
MySQL B: 1 table

Following instructions from the AWDWR book and any other source of  
information I’ve read I add an  
**ActiveRecord::Base.establish\_connection()** declaration in the model  
for the table in MySQL B.

Seems simple, but did not work. I tried adding the connection  
parameters inside the model and also using a symbol to reference a  
‘database.yml’ entry. Nothing.

Finally, when I was getting ready to ask for help I had the idea that  
made it work. I added **self.table\_name = ‘my\_db.my\_table\_name’** in  
every model (including the main DB, named ‘A’ in this example) and  
everything started working.

Second case scenario:  
MySQL: 6 tables  
Oracle: 1 table

Same as above but in addition to adding the **self.table\_name** stuff  
I needed to add in every table of the main (MySQL) DB an  
**ActiveRecord::Base.establish\_connection()** declaration pointing to  
either ‘development’, ‘test’ or ‘production’, depending on which DB I  
was using.

I thought that Rails would ‘know’ which DB to use. Am I wrong? Should  
I have added any type of setup value in ‘environment.rb’/other place  
to avoid all this?

Thanks.

Pepe

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 31, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/activerecord-not-as-smart-as-i-thought-it-would-be-or-is-it-me/169199/2 "2009-05-31T18:00:48Z")

</div>

Stupid question, did you do: “ActiveRecord::Base.establish\_connection”  
or did you just call “establish\_connection” in your model?

As you might imagine right now, calling  
“ActiveRecord::Base.establish\_connection” is very different than  
calling establish\_connection in the model you want to connect to  
another database.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 31, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/activerecord-not-as-smart-as-i-thought-it-would-be-or-is-it-me/169199/3 "2009-05-31T22:46:00Z")

</div>

Thanks Maurício,

For that extra table I used either  
ActiveRecord::Base.establish\_connection( :ube ) in the model, with the  
following in database.yml:  
ube:  
adapter: mysql  
database: ube  
user: root

or

ActiveRecord::Base.establish\_connection(  
:adapter =\> ‘mysql’,  
:database =\> ‘ube’,  
:user =\> ‘my\_user’ )  
in the model and no :ube declaration in database.yml

As I understand using ‘establish\_connection’ by itself would connect  
me to the ‘current’ DB, wouldn’t it?

Thanks.

Pepe

On May 31, 11:59 am, Maurício Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 31, 2009, 11:18pm UTC](https://www.ruby-forum.com/t/activerecord-not-as-smart-as-i-thought-it-would-be-or-is-it-me/169199/4 "2009-05-31T23:18:12Z")

</div>

Here’s how it would look like:

class YourModel \< ActiveRecord::Base  
establish\_connection :ube  
end

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [June 1, 2009, 12:05am UTC](https://www.ruby-forum.com/t/activerecord-not-as-smart-as-i-thought-it-would-be-or-is-it-me/169199/5 "2009-06-01T00:05:02Z")

</div>

So it was ME! 🙂

Thanks a lot Mauricio. As usual, it was so much simpler than I  
thought. Works like a charm.

Thanks!

Pepe

On May 31, 5:17 pm, Maurício Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
