class Module alias :normal_const_missing :const_missing def const_missing(class_id) begin return normal_const_missing(class_id) rescue end unless table_name = DrNicMagicModels::Schema.models[class_id] raise NameError.new("uninitialized constant #{class_id}") if DrNicMagicModels::Schema.models.enquired? class_id end klass_code = lambda {klass_code} klass = Class.new ActiveRecord::Base, &klass_code const_set class_id, klass klass.set_table_name table_name klass end end