# Set\_table\_name on the fly

**URL:** <https://www.ruby-forum.com/t/set-table-name-on-the-fly/172765>\
**Category:** Rails\
**Created:** [July 27, 2009, 7:26pm UTC](https://www.ruby-forum.com/t/set-table-name-on-the-fly/172765 "2009-07-27T19:26:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 27, 2009, 7:26pm UTC](https://www.ruby-forum.com/t/set-table-name-on-the-fly/172765/1 "2009-07-27T19:26:02Z")

</div>

Hi. I have a model that connects to a database, does something, connects  
to a different database, does something, and so on.

The switching of the databases appears to work, as calling:

MyModel.connection.instance\_eval { @config[:database] }

Yields the correct database.

After changing databases I also call:

set\_table\_name(‘new\_table\_name’)

However, upon issuing a query:

User.find(:all)

I receive an error similar to:

Unknown column ‘old\_table\_name.column’ in ‘where clause’…

Any ideas?

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 27, 2009, 9:04pm UTC](https://www.ruby-forum.com/t/set-table-name-on-the-fly/172765/2 "2009-07-27T21:04:04Z")

</div>

> Any ideas?

Yeah. 🙂

My initial post left out some important details. I wasn’t running:

User.find(:all)

I was running:

mode.association.find(:all)

And that’s where Rails is falling back on the inflected/guessed table  
name. I could probably change the has\_many directive to be dynamic, but  
I opted for something more like:

User.find(:all, :conditions =\> [‘associated\_id =\> ?’, id])
