Extend dynamically a model

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?

On 7/22/06, Magic 8 [email protected] wrote:

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?

set_table_name is a class method, so you ought to just be able to do
Account.set_table_name “whatever”

I don’t know for sure though as I’ve never tried it. I do imagine
that there would be ramifications if you just change the table name in
the middle of processing. Why is it that your accounts table needs to
change names while the app is running?

Pat

Magic 8 <[email protected]…> writes:

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…

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 K. mailto:[email protected]
http://www.dankohn.com/ tel:+1-415-233-1000

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs