# Dynamic set\_table\_name for achives

**URL:** <https://www.ruby-forum.com/t/dynamic-set-table-name-for-achives/55934>\
**Category:** Rails\
**Created:** [March 25, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/dynamic-set-table-name-for-achives/55934 "2006-03-25T15:11:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/82dd89/32.png) [@kris](https://www.ruby-forum.com/u/kris)\
**Post date:** [March 25, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/dynamic-set-table-name-for-achives/55934/1 "2006-03-25T15:11:37Z")

</div>

How can I pass something to set\_table\_name so that I can switch tables  
on the fly for accessing archive data?

If I could access sessions inside a model I would do something like:

if @session[:current\_period]  
set\_table\_name “statistics\_” + @session[:archive\_year]  
else  
set\_table\_name “statistics\_” + Date.now.year  
end

But since I cannot access the session from within a model how do I tell  
the model to archive data which is held in a different table eg.  
“statistics\_2005” ?

Many thanks, K.

---

<div class="post-metadata">

**Author:** ![kris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/82dd89/32.png) [@kris](https://www.ruby-forum.com/u/kris)\
**Post date:** [March 25, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/dynamic-set-table-name-for-achives/55934/2 "2006-03-25T19:44:01Z")

</div>

Kris wrote:

> How can I pass something to set\_table\_name so that I can switch tables  
> on the fly for accessing archive data?
> 
> If I could access sessions inside a model I would do something like:
> 
> if @session[:current\_period]  
> set\_table\_name “statistics\_” + @session[:archive\_year]  
> else  
> set\_table\_name “statistics\_” + Date.now.year  
> end
> 
> But since I cannot access the session from within a model how do I tell  
> the model to archive data which is held in a different table eg.  
> “statistics\_2005” ?
> 
> Many thanks, K.

Unless this is a legacy DB, I suggest changing it so you don’t have to  
have lots of statistics tables.

Otherwise(untested):  
#in your model  
def find\_correct\_table(sess)  
if sess[:current\_period]  
set\_table\_name “statistics\_#{sess[:archive\_year]}”  
else  
set\_table\_name “statistics\_#{Date.now.year}”  
end  
The in your controller:  
Model.find\_correct\_table(session)

That is untested btw.  
joey\_\_

---

<div class="post-metadata">

**Author:** ![kris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/82dd89/32.png) [@kris](https://www.ruby-forum.com/u/kris)\
**Post date:** [March 26, 2006, 12:07pm UTC](https://www.ruby-forum.com/t/dynamic-set-table-name-for-achives/55934/3 "2006-03-26T12:07:28Z")

</div>

Its not a legacy table, but because there is so much data it needs to be  
archived in to a seperate table for logical and performance reasons.

From your example it looks like you can use session’s in the model?

Many thanks.

joey\_\_ wrote:

> Kris wrote:
> 
> > How can I pass something to set\_table\_name so that I can switch tables  
> > on the fly for accessing archive data?
> > 
> > If I could access sessions inside a model I would do something like:
> > 
> > if @session[:current\_period]  
> > set\_table\_name “statistics\_” + @session[:archive\_year]  
> > else  
> > set\_table\_name “statistics\_” + Date.now.year  
> > end
> > 
> > But since I cannot access the session from within a model how do I tell  
> > the model to archive data which is held in a different table eg.  
> > “statistics\_2005” ?
> > 
> > Many thanks, K.
> 
> Unless this is a legacy DB, I suggest changing it so you don’t have to  
> have lots of statistics tables.
> 
> Otherwise(untested):  
> #in your model  
> def find\_correct\_table(sess)  
> if sess[:current\_period]  
> set\_table\_name “statistics\_#{sess[:archive\_year]}”  
> else  
> set\_table\_name “statistics\_#{Date.now.year}”  
> end  
> The in your controller:  
> Model.find\_correct\_table(session)
> 
> That is untested btw.  
> joey\_\_

---

<div class="post-metadata">

**Author:** ![kris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/82dd89/32.png) [@kris](https://www.ruby-forum.com/u/kris)\
**Post date:** [March 27, 2006, 10:34am UTC](https://www.ruby-forum.com/t/dynamic-set-table-name-for-achives/55934/4 "2006-03-27T10:34:01Z")

</div>

Ok I see the example is not using the session directly. It is being  
passed to a method which is setting the table name.

How would using it look?

@stats = Statistics.new  
@stats.find\_correct\_table(@session[:current\_period])  
@stats.find(:all)

^That wouldn’t work would it?

How about:

@stats =  
Statistics.find\_correct\_table(@session[:current\_period]).find(:all)

[By the way there was a typo in my code before so I dont need to pass  
the whole session to find\_correct\_table, just the current\_period var.]

Many thanks.

joey\_\_ wrote:

> Kris wrote:
> 
> > From your example it looks like you can use session’s in the model?
> 
> You can pass in the session hash as a paramter to a models method.
> 
> Joey\_\_

---

<div class="post-metadata">

**Author:** ![kris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/82dd89/32.png) [@kris](https://www.ruby-forum.com/u/kris)\
**Post date:** [March 26, 2006, 12:10pm UTC](https://www.ruby-forum.com/t/dynamic-set-table-name-for-achives/55934/5 "2006-03-26T12:10:19Z")

</div>

Kris wrote:

> From your example it looks like you can use session’s in the model?

You can pass in the session hash as a paramter to a models method.

Joey\_\_
