# Create rails model classes dynamically?

**URL:** <https://www.ruby-forum.com/t/create-rails-model-classes-dynamically/69250>\
**Category:** Ruby\
**Created:** [September 19, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/create-rails-model-classes-dynamically/69250 "2006-09-19T18:32:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 19, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/create-rails-model-classes-dynamically/69250/1 "2006-09-19T18:32:28Z")

</div>

Rails has a very nice database migration utility, where you can write  
something like this…

class CreateHouse \< ActiveRecord::Migration

def self.up  
create\_table :house do |t|  
t.column ‘person\_id’, :integer, :default =\> nil, :null =\> false  
t.column “created\_at”, :datetime  
t.column “name”, :string, :limit =\> 128, :default =\> “”  
end  
end

def self.down  
drop\_table :place  
end

end

Typically, you then create a class in the model directory that looks  
like this…

class House \< ActiveRecord::Base

belongs\_to :person

end

I’d like to dynamicallly generate this House class. With the help of  
this post, I can create the class and methods dynamically…  
[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/b969da2473e2c713/b8d076423378a2f2?lnk=gst&q=create+class&rnum=8#b8d076423378a2f2](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/b969da2473e2c713/b8d076423378a2f2?lnk=gst&q=create+class&rnum=8#b8d076423378a2f2)  
But I have not been able to figure out how to add the belongs\_to and  
has\_many style calls at the class level to the dynamicall generated  
class. Also, once this class is initially created dynamically,  
depending on what tables are created later, there may have to be  
additional belongs\_to and has\_many calls injected into the existing  
class, whew.

– any help would be great, thanks, Andrew

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 19, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/create-rails-model-classes-dynamically/69250/2 "2006-09-19T18:37:12Z")

</div>

unknown wrote:

> Rails has a very nice database migration utility, where you can write  
> something like this…

You will get better answers over on Rails lists.

[http://groups.google.com/group/rubyonrails-talk](http://groups.google.com/group/rubyonrails-talk)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 19, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/create-rails-model-classes-dynamically/69250/3 "2006-09-19T19:34:16Z")

</div>

unknown wrote:

> I’d like to dynamicallly generate this House class. With the help of  
> this post, I can create the class and methods dynamically…

[http://magicmodels.rubyforge.org/](http://magicmodels.rubyforge.org/)

I’m not into Rails yet, but I came across this the other day and tagged  
it as potentially useful in the future. I suspect it’s what you want.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/create-rails-model-classes-dynamically/69250/4 "2008-04-24T17:24:39Z")

</div>

unknown wrote:

> Rails has a very nice database migration utility, where you can write  
> something like this…
> 
> class CreateHouse \< ActiveRecord::Migration
> 
> def self.up  
> create\_table :house do |t|  
> t.column ‘person\_id’, :integer, :default =\> nil, :null =\> false  
> t.column “created\_at”, :datetime  
> t.column “name”, :string, :limit =\> 128, :default =\> “”  
> end  
> end
> 
> def self.down  
> drop\_table :place  
> end
> 
> end
> 
> Typically, you then create a class in the model directory that looks  
> like this…
> 
> class House \< ActiveRecord::Base
> 
> belongs\_to :person
> 
> end
> 
> I’d like to dynamicallly generate this House class. With the help of  
> this post, I can create the class and methods dynamically…  
> [http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/b969da2473e2c713/b8d076423378a2f2?lnk=gst&q=create+class&rnum=8#b8d076423378a2f2](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/b969da2473e2c713/b8d076423378a2f2?lnk=gst&q=create+class&rnum=8#b8d076423378a2f2)  
> But I have not been able to figure out how to add the belongs\_to and  
> has\_many style calls at the class level to the dynamicall generated  
> class. Also, once this class is initially created dynamically,  
> depending on what tables are created later, there may have to be  
> additional belongs\_to and has\_many calls injected into the existing  
> class, whew.
> 
> – any help would be great, thanks, Andrew

Hi,  
U said that u r able to create class dynamically i used same link but  
i m not able to create class dynamically please help me with some code  
snippet.  
Please help me i am facing this problem from last two weeeks??

Thanks  
varun

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/create-rails-model-classes-dynamically/69250/5 "2008-04-24T19:37:22Z")

</div>

- Varun G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (17:24) schrieb:

> Hi,  
> U said that u r able to create class dynamically i used same link but  
> i m not able to create class dynamically please help me with some code  
> snippet.  
> Please help me i am facing this problem from last two weeeks??

You can create a class by Class.new. For example:

c = Class.new do  
def moo  
“moo!”  
end  
end

obj = c.new  
puts obj.moo

mfg, simon … l

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 19, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/create-rails-model-classes-dynamically/69250/6 "2006-09-19T21:16:56Z")

</div>

wow…bulls eye !

Exactly what I am looking for.

Thanks again Internet

– Andrew
