From: Brian Candler Date: 2009-01-29T04:06:59+09:00 Subject: Re: Defining an ActiveRecord class within a method of another class Glenn wrote: > Hi, > > I want to create a class and define a subclass of ActiveRecord within > that class, like this: > > class Results > def initialize(db) > class db < ActiveRecord::Base > end > end > end > > r1 = Results.new('DM') > r2 = Results.new('AE') Class.new(superclass) should do what you need without need to eval anything. This gives you an anonymous class, which you can then assign to a constant to give it a proper class name. Something like (untested): class Results def initialize(name) @klass = Results.const_set(name, Class.new(ActiveRecord::Base)) end def query @klass.find(...) end end r1 = Results.new('DM') Results::DM.find(...) -- Posted via http://www.ruby-forum.com/.