Re: dynamically defined ar classes

Steve Bartholomew
Tue, 30 Jan 2007 20:18:29 -0000

> So: can this work?
Yeah, but not in the way you have.  You can use the ''eval''
command to
execute ruby code to create your classes:

[''MyNewClass''].each do |klass|
   model_string = "class #{klass} < ActiveRecord::Base; def say_hello;
''Hello!''; end; end;"
   eval(model_string)
end

my_new_class = MyNewClass.new
my_new_class.say_hello -> "Hello!"

You can build up the string using the values defined in your hashes
and arrays then execute.  I''m not sure what effect all this will have
on the execution time of you app, but try it out and see how you get
on.

Steve