# Set\_table\_name woes, fragmented domain class

**URL:** <https://www.ruby-forum.com/t/set-table-name-woes-fragmented-domain-class/163186>\
**Category:** Rails\
**Created:** [March 18, 2009, 3:42am UTC](https://www.ruby-forum.com/t/set-table-name-woes-fragmented-domain-class/163186 "2009-03-18T03:42:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_JSISJacks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@Michael\_JSISJacks](https://www.ruby-forum.com/u/Michael_JSISJacks)\
**Post date:** [March 18, 2009, 3:42am UTC](https://www.ruby-forum.com/t/set-table-name-woes-fragmented-domain-class/163186/1 "2009-03-18T03:42:59Z")

</div>

Hi all,

I’m running Rails on top of a Mongrel cluster. I have several model  
classes that are going to have so many records it is not feasible to  
keep them all in the same table. So I’ve split out the records into  
many different tables, each with its own suffix which corresponds to  
the id of the model to which all records in that particular table  
belong.

For example, if I have a Domain AR object with an id of 20, all  
DomainURL objects that :belong\_to that particular Domain object are  
stored in table domain\_urls\_20.

This necessitates a call to set\_table\_name before the DomainURL  
objects are fetched, like so:

DomainURL.set\_table\_name “domain\_urls\_” + domain.id

However, when I go into production mode, I get mismatched table and  
column names on my find’s. I am assuming this is caused by  
ActiveRecord caching the domain model so that set\_table\_name is  
rendered ineffective when it goes to fetch a record. However, I’m not  
quite sure if this is the case.

If anyone could shed some light here, or possibly suggest an  
alternative method of accomplishing my objective, I’m all ears.

Thanks,

Michael J. I. Jackson  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Michael\_JSISJacks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@Michael\_JSISJacks](https://www.ruby-forum.com/u/Michael_JSISJacks)\
**Post date:** [March 19, 2009, 5:25am UTC](https://www.ruby-forum.com/t/set-table-name-woes-fragmented-domain-class/163186/2 "2009-03-19T05:25:49Z")

</div>

Michael J. I. Jackson wrote:

> This necessitates a call to set\_table\_name before the DomainURL  
> objects are fetched, like so:
> 
> DomainURL.set\_table\_name “domain\_urls\_” + domain.id
> 
> However, when I go into production mode, I get mismatched table and  
> column names on my find’s. I am assuming this is caused by  
> ActiveRecord caching the domain model so that set\_table\_name is  
> rendered ineffective when it goes to fetch a record. However, I’m not  
> quite sure if this is the case.

Perhaps your problem is caching of table names by the urls  
association. You could clear the cache using something like

class Domain  
has\_many :urls, :class\_name =\> ‘DomainURL’

def set\_url\_table  
self.class.reflect\_on\_association(:urls).instance\_eval do  
@table\_name = @quoted\_table\_name = nil  
end  
DomainURL.set\_table\_name “domain\_urls\_” + id  
end  
end

domain = Domain.find(…)  
domain.set\_url\_table  
urls = domain.urls

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
