Re: dynamic tablenames

John-Scott
Fri, 26 Jan 2007 21:22:38 -0000

On Jan 23, 9:31 am, "Russell Norris"
<sco...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> I''m doing a similar thing [setting information about the database
> on-the-fly] but I''m having to change a model''s
table_name_prefix. The one
> concern I have [though it hasn''t come up so far] is the
possibility of two
> requests being made at the same or close to the same time and one
mistakenly
> using the others'' setup. I''m thinking of using
transactions, perhaps. I
> dunno but I too am interested in if anyone else has done on-the-fly type
> database configuring.
>
> RSL

How are you all setting table_name or table_name_prefix dynamically? I
initially thought I could just set a session variable (since I want
this value to persist for a given user across multiple requests until
it is reset by the user) and then use in my model:
class TestTable < ActiveRecord::Base
  self.table_name_prefix = session[:my_little_variable] + ''_''
end

But this doesn''t work, since apparently session data is not directly
available in the models. Not every model I''m working with needs a table
prefix, so I don''t want to set this globally. How do I take a variable
selected by the user and cleanly use this as a table name prefix for
the models that need it?