# Model Inheritance Scope

**URL:** <https://www.ruby-forum.com/t/model-inheritance-scope/143763>\
**Category:** Deployment\
**Created:** [July 22, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/model-inheritance-scope/143763 "2008-07-22T16:01:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aawilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@aawilson](https://www.ruby-forum.com/u/aawilson)\
**Post date:** [July 22, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/model-inheritance-scope/143763/1 "2008-07-22T16:01:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![aawilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@aawilson](https://www.ruby-forum.com/u/aawilson)\
**Post date:** [July 22, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/model-inheritance-scope/143763/2 "2008-07-22T18:20:34Z")

</div>

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.
