Forum: Ruby on Rails dynamic set_table_name for achives

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kris (Guest)
on 2006-03-25 16:11
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.
joey__ (Guest)
on 2006-03-25 20:44
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__
Kris (Guest)
on 2006-03-26 14:07
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__
joey__ (Guest)
on 2006-03-26 14:10
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__
Kris (Guest)
on 2006-03-27 12:34
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__
This topic is locked and can not be replied to.