# Single-table inheritance: Uninitialized class constant

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance-uninitialized-class-constant/153079>\
**Category:** Rails\
**Created:** [November 11, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-uninitialized-class-constant/153079 "2008-11-11T22:36:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fritza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5f9b8f/32.png) [@fritza](https://www.ruby-forum.com/u/fritza)\
**Post date:** [November 11, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-uninitialized-class-constant/153079/1 "2008-11-11T22:36:05Z")

</div>

This forum has been of no help before, but I am nothing daunted.

I have a “people” table, mapping to a Person ActiveRecord class, which  
has two subclasses, Principal and Secondary. There is a type column.

There is a “locations” table, mapping to Location. Location belongs\_to  
Principal. Principal has\_many Locations. There is a principal\_id column  
in locations.

There is a LocationsController. It renders rhtml, which contains  
something like

\<%= @location.principal.full\_name %\>

… to which Rails complains “uninitialized constant  
Location::Principal”. locations\_controller.rb does not refer to  
Principal. The problem must come from the class information in the  
relationship, or possibly in the STI type information.

If I change the belongs\_to to:

belongs\_to :principal, :class\_name =\> “Person”, :foreign\_key =\>  
“principal\_id”

it works, but this bothers me. It is not what I intend. Locations belong  
to Principals, not to People in general.

The alternative, I think, is to have locations\_controller.rb require  
‘person’, but that gives me problems with reloading in development mode.

What should I do?

$ ruby --version  
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]  
$ rails --version  
Rails 1.2.6

---

<div class="post-metadata">

**Author:** ![fritza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5f9b8f/32.png) [@fritza](https://www.ruby-forum.com/u/fritza)\
**Post date:** [November 11, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-uninitialized-class-constant/153079/2 "2008-11-11T22:52:21Z")

</div>

On 11 Nov 2008, at 21:36, Fritz A. wrote:

> The alternative, I think, is to have locations\_controller.rb require  
> ‘person’, but that gives me problems with reloading in development  
> mode.

use require\_dependency instead of require (and i would do this at the  
top of the location model, not in the controller)

Fred

---

<div class="post-metadata">

**Author:** ![fritza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5f9b8f/32.png) [@fritza](https://www.ruby-forum.com/u/fritza)\
**Post date:** [November 12, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-uninitialized-class-constant/153079/3 "2008-11-12T21:18:19Z")

</div>

Frederick C. wrote:

> use require\_dependency instead of require (and i would do this at the  
> top of the location model, not in the controller)

This was exactly what I needed. Thank you.

It’s too bad that Pickaxe (2nd ed) and Agile Web D. with Rails  
(2nd ed) didn’t mention require\_dependency. Can anyone recommend a  
reference that would have included it?

```
â€”Â F
```

---

<div class="post-metadata">

**Author:** ![fritza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5f9b8f/32.png) [@fritza](https://www.ruby-forum.com/u/fritza)\
**Post date:** [November 12, 2008, 11:37pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-uninitialized-class-constant/153079/4 "2008-11-12T23:37:51Z")

</div>

On Nov 12, 8:18 pm, Fritz A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Frederick C. wrote:
> 
> > use require\_dependency instead of require (and i would do this at the  
> > top of the location model, not in the controller)
> 
> This was exactly what I needed. Thank you.
> 
> It’s too bad that Pickaxe (2nd ed) and Agile Web D. with Rails  
> (2nd ed) didn’t mention require\_dependency. Can anyone recommend a  
> reference that would have included it?

Well it’s a rails only thing so the Pickaxe would never cover it and  
it’s also a bit of slightly hairy internal stuff. “Luke, Use the  
source!”

Fred
