# Extend dynamically a model

**URL:** <https://www.ruby-forum.com/t/extend-dynamically-a-model/99411>\
**Category:** Rails\
**Created:** [July 22, 2006, 12:10pm UTC](https://www.ruby-forum.com/t/extend-dynamically-a-model/99411 "2006-07-22T12:10:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Magic\_8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@Magic\_8](https://www.ruby-forum.com/u/Magic_8)\
**Post date:** [July 22, 2006, 12:10pm UTC](https://www.ruby-forum.com/t/extend-dynamically-a-model/99411/1 "2006-07-22T12:10:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Magic\_8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@Magic\_8](https://www.ruby-forum.com/u/Magic_8)\
**Post date:** [July 22, 2006, 12:28pm UTC](https://www.ruby-forum.com/t/extend-dynamically-a-model/99411/2 "2006-07-22T12:28:37Z")

</div>

On 7/22/06, Magic 8 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Magic\_8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@Magic\_8](https://www.ruby-forum.com/u/Magic_8)\
**Post date:** [July 26, 2006, 4:37am UTC](https://www.ruby-forum.com/t/extend-dynamically-a-model/99411/3 "2006-07-26T04:37:42Z")

</div>

Magic 8 \<unfilled@…\> 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…

---

<div class="post-metadata">

**Author:** ![Magic\_8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@Magic\_8](https://www.ruby-forum.com/u/Magic_8)\
**Post date:** [July 26, 2006, 7:19am UTC](https://www.ruby-forum.com/t/extend-dynamically-a-model/99411/4 "2006-07-26T07:19:19Z")

</div>

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/Base.html#M000880)  
[http://api.rubyonrails.com/classes/ActiveRecord/Migration.html](http://api.rubyonrails.com/classes/ActiveRecord/Migration.html)

What application did you envision for dynamically created tables?

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000
