# STI isn't working for me

**URL:** <https://www.ruby-forum.com/t/sti-isnt-working-for-me/67267>\
**Category:** Rails\
**Created:** [September 1, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/sti-isnt-working-for-me/67267 "2006-09-01T18:43:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [September 1, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/sti-isnt-working-for-me/67267/1 "2006-09-01T18:43:08Z")

</div>

i am trying to use single table inheritance for the first time and not  
having much luck so far.

in my model i have:

class NavigationItem \< ActiveRecord::Base  
end

class MenuItem \< NavigationItem  
end

class LinkItem \< NavigationItem  
end

but when i use the console to create a new record like menu =  
MenuItem.new i get an error saying: NameError: uninitialized constant  
MenuItem

am i doing something wrong?

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [September 1, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/sti-isnt-working-for-me/67267/2 "2006-09-01T19:11:07Z")

</div>

Hey Josh,  
Have a look at the following wiki;  
[http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance](http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance),  
specifically the ‘note’ part.\* “Note:\* Rails must â??seeâ?? the file  
containing the STI classes before it can use them, otherwise youâ??ll end  
up with a â??uninitialized constantâ? error. This can be a problem if the  
name of the class and the name of the file defining it are not the same,  
e.g. you have the model â??Managerâ?? in the file â??employees.rbâ??. Rails will  
not be able to divine the filename from the class name in this case.”

```
I take it that this is what is biting you :)
Regards
Stef

```

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [September 1, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/sti-isnt-working-for-me/67267/3 "2006-09-01T20:07:23Z")

</div>

yes, that is what i was doing. so what is the better practice? on the  
wiki it suggests adding â??model :employeesâ? to the application.rb, but it  
sounds like i could do that or just put the classes in separate files  
that have matching names?

Stef Telford wrote:

> Hey Josh,  
> Have a look at the following wiki;  
> [http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance](http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance),  
> specifically the ‘note’ part.\* “Note:\* Rails must â??seeâ?? the file  
> containing the STI classes before it can use them, otherwise youâ??ll end  
> up with a â??uninitialized constantâ? error. This can be a problem if the  
> name of the class and the name of the file defining it are not the same,  
> e.g. you have the model â??Managerâ?? in the file â??employees.rbâ??. Rails will  
> not be able to divine the filename from the class name in this case.”
> 
> ```
> I take it that this is what is biting you :)
> Regards
> Stef
> 
> ```

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [September 2, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/sti-isnt-working-for-me/67267/4 "2006-09-02T19:06:45Z")

</div>

Josh K. wrote:

> yes, that is what i was doing. so what is the better practice? on the  
> wiki it suggests adding “model :employees” to the application.rb, but it  
> sounds like i could do that or just put the classes in separate files  
> that have matching names?

Yes put them in separate files with matching names. One model per file  
please:)

Peter

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [September 1, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/sti-isnt-working-for-me/67267/5 "2006-09-01T20:51:20Z")

</div>

Hey Josh,  
Well, personally, I am all for thinking about the person that comes  
-after- you (so to speak). Putting them into seperate model files is  
probably ‘saner’ in my view. Even if your only doing this on your own  
application, its probably a good habit to get into … but … thats jst  
my 2cents and all coding style is highly subjective. I would hate to  
come across as a pythonista 😜 😛

```
Regards
Stef
```
