Dynamic set_table_name for achives


#1

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.


#2

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__


#3

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__


#4

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__


#5

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__