# Dynamic Table Names for Daily Tables

**URL:** <https://www.ruby-forum.com/t/dynamic-table-names-for-daily-tables/168400>\
**Category:** Rails\
**Created:** [May 20, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/dynamic-table-names-for-daily-tables/168400 "2009-05-20T12:15:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul-au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@paul-au](https://www.ruby-forum.com/u/paul-au)\
**Post date:** [May 20, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/dynamic-table-names-for-daily-tables/168400/1 "2009-05-20T12:15:21Z")

</div>

Hi guys,

I have a problem with a legacy database that has a sequence of daily  
tables (generated elsewhere) of the form

Header090501  
Daily090501  
Extra090501  
Detail090501  
Header090502  
Daily090502  
Extra090502  
Detail090502  
Header090503… etc.

In addition, I’m using ar-extensions and composite primary keys, with a  
slight monkeypatch to get them working together for bulk inserts.

My definition is currently like:

module Legacy  
class Header \< ActiveRecord::Base  
set\_table\_name “Header#{Time.now.strftime(”%y%m%d")}"  
set\_primary\_keys “ResultID”  
end  
end

All this is working fine, right up until midnight, when the tables  
should change, but of course my definition is only dynamic if it is run  
again. Since I have long running jobs that need to run across the time  
boundaries, I thought that the best thing to do would be to call

Header::set\_table\_name

from the initialization of the interface that first attaches to the  
database. This isn’t strictly speaking Rails code, but relies heavily on  
ActiveRecord, ActiveSupport etc. Is there a more conventional way to  
handle this sort of thing?

Mac
