Re: dynamic tablenames

Russell Norris
Sat, 27 Jan 2007 14:26:25 -0000

What''s the criteria for which models need a new table name/prefix? If
it
centers around another model then you can put a table_prefix attribute on
that model and call it thusly:

def self.set_tables
  OtherModel.set_table_name "#{table_prefix}_othermodels"
end

Don''t use table_name_prefix as it sets ActionRecord::Base itself and
not the
specific model as you might think. Lots of fun confusion on that for me.
Also, you can''t [or I couldn''t figure out how to] set the
table_name on the
model itself. For me, the models that needed new table names [databases
actually] were all related to what site was being used so my Site model has
the set_tables method. Call that method from a filter on the appropriate
controller methods and voila! On-the-fly table name changes. If you have a
relationship between the models, you might also need to tweak your
associations code like this:

class Whatever
  has_many :othermodels, :finder_sql => ''SELECT
#{table_name}_othermodels.*
FROM #{table_name}_othermodels WHERE #{table_name}_othermodels.whatever_id
#{id}''

I hope that''s not too obtuse. It''s just that my code uses this
functionality
to change databases and I don''t want to paste it and have you thinking
that
it''s merely for changing table names. However, if anyone is
wondering... If
you do need to change the database, you don''t have to establish a new
connection but can use Model.set_table_name "#{db_name}.table_name".
You
can use the same trick in the association to select across two databases.

God, I hope that wasn''t too obscure. Apologies in advance.

RSL

On 1/26/07, John-Scott
<john.scott.atlakson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
>
>
> 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?