# Re: Ensuring AR#set\_table\_name is valid with multiple users

**URL:** <https://www.ruby-forum.com/t/re-ensuring-ar-set-table-name-is-valid-with-multiple-users/118309>\
**Category:** Rails\
**Created:** [October 25, 2007, 3:34pm UTC](https://www.ruby-forum.com/t/re-ensuring-ar-set-table-name-is-valid-with-multiple-users/118309 "2007-10-25T15:34:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 25, 2007, 3:34pm UTC](https://www.ruby-forum.com/t/re-ensuring-ar-set-table-name-is-valid-with-multiple-users/118309/1 "2007-10-25T15:34:53Z")

</div>

Hi

You would put the set\_table\_name definition in the model at the top -  
just below the class definition line, so you file will look like this:

class ModelTable \< ActiveRecord::Base  
set\_table\_name ‘MyTableName’

end

As the definition is in the model, then your concern regarding the  
overwriting of the table names will not be valid - the model takes care  
of this - each model will have its own set\_table\_name call. The model  
models only a single table, and so you only need one set\_table\_name per  
model.

HTH

Clive

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 25, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/re-ensuring-ar-set-table-name-is-valid-with-multiple-users/118309/2 "2007-10-25T16:00:37Z")

</div>

On Oct 25, 2:34 pm, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Unfortunately (and I understand this goes against the ‘opionionated’  
approach of RoR) I would like one model to map onto multiple tables.  
My rationale: these tables have very minor differences, if any, and  
there are hundreds of them, I don’t think that one model per table is  
a realistic option. Each table contains on the order of 100,000  
records, some more, many less - the original DB designer sought to  
minimise the size of the tables, so he effectively ‘sharded’ or broke  
up what could have been a very large table. He also took the  
opportunity to add or modify columns as each new table was generated.

The tables are generated by a process outside the control of my web  
application, I just have to read from them.

I know that AR can map onto the varying tables so a single model that  
did that seemed to fit nicely, I just had to change the table name and  
reread the column info. I hope this explains my approach.

I have not considered dynamically generating a model for the table on  
each web call! But I guess I could try some metaprogramming and  
generate a model per table - but I’d prefer to avoid that if possible!

Any other ideas welcomed.

Allan

Allan

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 25, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/re-ensuring-ar-set-table-name-is-valid-with-multiple-users/118309/3 "2007-10-25T17:54:05Z")

</div>

On 25 Oct 2007, at 14:59, Allan wrote:

> I know that AR can map onto the varying tables so a single model that  
> did that seemed to fit nicely, I just had to change the table name and  
> reread the column info. I hope this explains my approach.

Each mongrel (or fastcgi instance etc…) only handles a single  
request at a time, so you’ll never have the problem where 1 request  
changes the value of table\_name while a second request assumed it had  
set it correctly

> I have not considered dynamically generating a model for the table on  
> each web call! But I guess I could try some metaprogramming and  
> generate a model per table - but I’d prefer to avoid that if possible!

Dr Nic’s magic models does something vaguely similar. If you type  
Post, it hooks into const\_missing, sees that there’s a posts table  
and creates the Post class for you. Not quire what you need, since  
your table names aren’t what it expects, but might give you some ideas.

assuming all you actual functionality is in ModelTable, you could do  
this

class ModelTable \< ActiveRecord::Base  
self.abstract\_class = true

…  
end

and then create subclasses of model table:

models\_and\_tables.each do |model\_table|  
klass = Class.new(ModelTable)  
Object.const\_set(model\_table[0], klass)  
klass.set\_table\_name model\_table[1]  
end

assuming models\_and\_tables is an array of model name/table name pairs

Fred
