Unable to register constants

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
 
P

Pit Capitain

Matthew said:
Hi guys... not sure why setting constants here isn't working!?! Here's
the code:

(...)

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...

Matt, your code (minus the Rails dependencies) did work for me with a
very simple test case. Could you show us your test program?

Regards,
Pit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top