Unable to register constants

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

Matthew P. schrieb:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs