# How can I dynamiclly generate models?

**URL:** <https://www.ruby-forum.com/t/how-can-i-dynamiclly-generate-models/96112>\
**Category:** Rails\
**Created:** [June 26, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/how-can-i-dynamiclly-generate-models/96112 "2006-06-26T14:46:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cafebabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@cafebabe](https://www.ruby-forum.com/u/cafebabe)\
**Post date:** [June 26, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/how-can-i-dynamiclly-generate-models/96112/1 "2006-06-26T14:46:40Z")

</div>

I am trying to write a plugin, ‘acts\_as\_commentable’, for my models -  
Image, Book, Music and so on, I do not want to use polymorphic  
association, so very model should have its own comment class.  
Here is my code

acts\_as\_commentable.rb  
module Commentable

```
def self.included(base)
    base.extend(ClassMethods)
end

module ClassMethods
    def acts_as_commentable(options = {})
        class_def = "class Comment < ActiveRecord::Base
            set_table_name '#{self.name.downcase}_comments'

            belongs_to :user, :class_name=>'::User'
            belongs_to :#{self.name.downcase}, 

```

:class\_name=\>’::#{self}’  
end"  
self.class\_eval class\_def

```
        has_many :comments, :class_name=>"#{self.name}::Comment", 

```

:order=\>‘created\_at desc’, :dependent =\> :delete\_all

```
        extend Commentable::SingletonMethods
        include Commentable::InstanceMethods
    end
end

module SingletonMethods
end

module InstanceMethods
end

```

end

init.rb  
require ‘acts\_as\_commentable’  
ActiveRecord::Base.send(:include, Commentable)

image.rb  
acts\_as\_commentable

This results in Image::Comment, Book::Comment…(I have no idea about  
how defind top level classes ImageComment, BookComment… ☹ )

and my controller:  
def ImageController  
def view  
…  
@comment=Event::Comment.new #line 45  
…  
end  
end

everything is ok when I start webrick and direct to this page, but when  
I refresh my brower, an error ocurr:  
NameError in ImageController#view  
uninitialized constant Comment  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:100:in  
`const_missing' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in`const\_missing’  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:133:in  
`const_missing' #{RAILS_ROOT}/app/controllers/image_controller.rb:45:in`view’

Anyone know what’s the problem, thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![cafebabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@cafebabe](https://www.ruby-forum.com/u/cafebabe)\
**Post date:** [June 26, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/how-can-i-dynamiclly-generate-models/96112/2 "2006-06-26T17:04:00Z")

</div>

Image.find(1).comments() and Image::Comment.new work in the console.

It seems people like generating model methods/attrs much more than  
generating the whole model classes ☹

---

<div class="post-metadata">

**Author:** ![cafebabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@cafebabe](https://www.ruby-forum.com/u/cafebabe)\
**Post date:** [June 27, 2006, 5:28am UTC](https://www.ruby-forum.com/t/how-can-i-dynamiclly-generate-models/96112/3 "2006-06-27T05:28:40Z")

</div>

I add

def self.reloadable?  
false  
end

to the Comment class definition, it works now 🙂  
and I find

class ::ImageComment  
end

defines a top level class…

---

<div class="post-metadata">

**Author:** ![cafebabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@cafebabe](https://www.ruby-forum.com/u/cafebabe)\
**Post date:** [June 27, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/how-can-i-dynamiclly-generate-models/96112/4 "2006-06-27T17:37:00Z")

</div>

Hey,

I synthesize classes in my acts\_as\_enumerated plugin. You can see  
how I do it here:

[http://svn.protocool.com/rails/plugins/enumerations\_mixin/trunk/lib/](http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk/lib/)  
active\_record/virtual\_enumerations.rb

In the “synthesize\_if\_defined” method I create the toplevel class  
like this:

eval(class\_def, TOPLEVEL\_BINDING)

## Hope this helps, Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![cafebabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@cafebabe](https://www.ruby-forum.com/u/cafebabe)\
**Post date:** [June 28, 2006, 2:44am UTC](https://www.ruby-forum.com/t/how-can-i-dynamiclly-generate-models/96112/5 "2006-06-28T02:44:51Z")

</div>

> eval(class\_def, TOPLEVEL\_BINDING)

This is very much appreciated. Thank you!

---

<div class="post-metadata">

**Author:** ![cafebabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@cafebabe](https://www.ruby-forum.com/u/cafebabe)\
**Post date:** [June 28, 2006, 5:31am UTC](https://www.ruby-forum.com/t/how-can-i-dynamiclly-generate-models/96112/6 "2006-06-28T05:31:46Z")

</div>

thanks  
this following code also works

class OuterClass  
…  
class\_eval "class ::TopLevelCalss  
end  
"  
…  
end
