I want to change the table name of the ActiveRecord model class during request dynamically.
For example, there are many tables having similar structure(columns):
mydb:
sample_data_12222
sample_data_12223
sample_data_12224
sample_data_12225
...
So, what I want to do is...
_1. Defining the base model class like:
class SampleData < ActiveRecord::Base
_2. Changing the target table during request like:
def action_method
SampleData.set_table_name "sample_data_#{params[:id]}"
@rows = SampleData.all
It seems that above code is right if it's run on non-threaded environment (like on Passenger/mod_rails). But it's not thread-safe, so it may not work on threaded-environment (like on JRuby-Rack).
Also I tried creating the delived class like this:
def action_method
@model_class = Class.new(SampleData)
@model_class.set_table_name "sample_data_#{params[:id]}"
@rows = @model_class.all
But it cause memory leaks, though the delived model class is not used any more after request was completed. :(
Is there a better way to do that?