Model Inheritance Scope

I have a method that creates an instance of a form from a class. The
class the form is created from is from a hierarchy that exists through
single-table inheritance in our database. Since instances of forms are
not actually form objects, the type of form each instance is created
from is stored as a string in the form object in the table, and we call
the following to create the new instance:

cls = form.instance_class.nil? ? FormInstance :
eval(form.instance_class)
cls.new(attrs.merge(:form => form))

The problem is that eval always raises a NameError with the message
“uninitialized constant FormInstance::SavedForm”, which indicates that
SavedForm (in this case, inherited from FormInstance) is not in the
appropriate scope. I’m sure I could stick the name of all of
FormInstance subclasses in FormInstance, but why should I even have to?
Why isn’t SavedForm in scope, and what can I do to get Ruby to determine
that, yes, it should be in scope, without specifically telling it so?
We might be adding to this hierarchy later, and I (or, more poignantly,
the project’s future maintainers) would hate to have to go through each
model and change the lists every time the hierarchy changes. This seems
like it would come up pretty often, and I’m sure an easy solution exists
that I don’t know about. Can anyone help?

I posted this in the wrong forum, sorry. I also discovered that I had
an extra space in a filename, so it’s now working. Woo.

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