M
Matthew Painter
Hi guys... not sure why setting constants here isn't working!?! Here's
the code:
class Module
@@dynamically_created_models ||= Hash.new
alias :normal_const_missing :const_missing
def const_missing(class_id)
begin
return normal_const_missing(class_id)
rescue
end
puts "Looking for unintialized model #{class_id}"
if @@dynamically_created_models[class_id]
puts "*** Constant not set correctly, getting from class
variable..."
return @@dynamically_created_models[class_id]
end
# Can we auto generate this model class?
unless table_name = DrNicMagicModels::Schema.models[class_id]
raise NameError.new("uninitialized constant #{class_id}")
end
klass = Class.new(ActiveRecord::Base)
klass.class_eval do
set_table_name table_name
end
@@dynamically_created_models[class_id] = klass
const_set class_id, klass
return const_get(class_id) rescue raise("Cannot set constant
#{class_id}")
end
end
Sure enough, when i run a small test program, part of the output is:
Looking for unintialized model Group
*** Constant not set correctly, getting from class variable...
Any help gratefully received!
Thanks,
Matt
the code:
class Module
@@dynamically_created_models ||= Hash.new
alias :normal_const_missing :const_missing
def const_missing(class_id)
begin
return normal_const_missing(class_id)
rescue
end
puts "Looking for unintialized model #{class_id}"
if @@dynamically_created_models[class_id]
puts "*** Constant not set correctly, getting from class
variable..."
return @@dynamically_created_models[class_id]
end
# Can we auto generate this model class?
unless table_name = DrNicMagicModels::Schema.models[class_id]
raise NameError.new("uninitialized constant #{class_id}")
end
klass = Class.new(ActiveRecord::Base)
klass.class_eval do
set_table_name table_name
end
@@dynamically_created_models[class_id] = klass
const_set class_id, klass
return const_get(class_id) rescue raise("Cannot set constant
#{class_id}")
end
end
Sure enough, when i run a small test program, part of the output is:
Looking for unintialized model Group
*** Constant not set correctly, getting from class variable...
Any help gratefully received!
Thanks,
Matt