It''s deeply unlikely that you want to create new tables on the fly.
Although I''m sure there''s a ruby way of doing so
(it''s just a kind of
reflection), the whole rails framework is designed around having
stable tables per revision of your app. set_table_name allows you to
map legacy table names to a nicer model name. It''s not designed to
be called from a controller. If you wanted to get wild with changing
the database from the controller, you''d want to use migration methods.
http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000880
http://api.rubyonrails.com/classes/ActiveRecord/Migration.html
What application did you envision for dynamically created tables?
- dan
--
Dan Kohn <mailto:dan@dankohn.com>
<http://www.dankohn.com/> <tel:+1-415-233-1000>
On Jul 25, 2006, at 7:54 AM, j s wrote:
> Magic 8 <unfilled@...> writes:
>
>>
>> Hello,
>> I''ve a model defined as
>>
>> class Account < ActiveRecord::Base
>> end
>>
>> This maps a mysql table Accounts. I want to be a able to dynamically
>> change this model. During the life of my app, the table Accounts may
>> change name, how do I do a set_table_name not within account.rb?
>>
>
>
> I am trying to do the same thing! The reason is that I want to be
> able to create
> new tables on the fly in my application and have models with which
> I can
> manipulate them.
>
> So I''ve experimented with using set_table_name multiple times in a
> function.
> Unfortunately, it appears that the first time you call new on your
> model, it
> finalizes the name of the table name for all subsequent calls to
> new. On the
> other hand, when you call table_name, it will give you whatever the
> latest table
> name you''ve set is. I wonder if this is a bug. Example, from the
> consol:
>
> Loading development environment.
>>> GenericName.table_name
> => "posts"
>>> GenericName.set_table_name("generic_names")
> => nil
>>> GenericName.table_name
> => "generic_names"
>>> GenericName.new
> => #<GenericName:0xb77eeb54 @new_record=true, @attributes=
> {"last_version_id"=>0,
> "first_version_id"=>0, "data"=>""}>
>>> GenericName.set_table_name("posts")
> => nil
>>> GenericName.set_table_name("posts")
> => nil
>>> GenericName.set_table_name("posts")
> => nil
>>> GenericName.table_name
> => "posts"
>>> GenericName.new
> => #<GenericName:0xb77dee84 @new_record=true, @attributes=
> {"last_version_id"=>0,
> "first_version_id"=>0, "data"=>""}>
>>> ^C
>>>
>
> Notice that the object returned by the second call to new is still
> from the
> generic_names table, even though I''ve changed the name to posts.
>
> Anyone know how to fix this problem? Perhaps it''s not possible...
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails