# How to dynamically set table\_name of model object without concurrency issues?

**URL:** <https://www.ruby-forum.com/t/how-to-dynamically-set-table-name-of-model-object-without-concurrency-issues/135008>\
**Category:** Rails\
**Created:** [April 17, 2008, 1:06am UTC](https://www.ruby-forum.com/t/how-to-dynamically-set-table-name-of-model-object-without-concurrency-issues/135008 "2008-04-17T01:06:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrikShun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@FrikShun](https://www.ruby-forum.com/u/FrikShun)\
**Post date:** [April 17, 2008, 1:06am UTC](https://www.ruby-forum.com/t/how-to-dynamically-set-table-name-of-model-object-without-concurrency-issues/135008/1 "2008-04-17T01:06:17Z")

</div>

Hello all,

I’m trying to figure out a good way to dynamically set the table\_name  
of an ActiveRecord model object.

I’ve got a legacy db with multiple tables which hold the same type of  
data. The tables are laid out as DATA1000 through DATA9999.

My app needs to allow the user to select a number 1000 through 9999  
and then use that selection in order to determine the appropriate  
table to query.

I have a single ActiveRecord object called DataX which I’d like to use  
to access all of the above tables.

What I’ve been doing is to call DataX.set\_table\_name for every search  
depending on the users selection to get the data from the right table.

This is not going to work in a multi-user env as set\_table\_name is a  
class method and I’d be resetting the table name for all users, all  
sessions.

Does anyone have any other suggestions for dynamically assigning a new  
table name to an instance of an ActiveRecord that is scoped at the  
user session level?

Thanks,

Allen

---

<div class="post-metadata">

**Author:** ![FrikShun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@FrikShun](https://www.ruby-forum.com/u/FrikShun)\
**Post date:** [April 17, 2008, 9:20am UTC](https://www.ruby-forum.com/t/how-to-dynamically-set-table-name-of-model-object-without-concurrency-issues/135008/2 "2008-04-17T09:20:27Z")

</div>

On Apr 17, 12:05 am, FrikShun [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> table to query.
> 
> I have a single ActiveRecord object called DataX which I’d like to use  
> to access all of the above tables.

Make DataX abstract and dynamically create subclasses as needed ? It’s  
not pretty but given that (right now) a single instance (mongrel  
process, fastcgi listener etc…) of rails only processes one request  
at a time you could probably get away with calling set\_table\_name on  
DataX

Fred
