# Single Table Inheritance

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance/107739>\
**Category:** Rails\
**Created:** [June 25, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/single-table-inheritance/107739 "2007-06-25T21:31:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serengeti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e495f1/32.png) [@Serengeti](https://www.ruby-forum.com/u/Serengeti)\
**Post date:** [June 25, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/single-table-inheritance/107739/1 "2007-06-25T21:31:34Z")

</div>

I am trying to implement single table inheritance according to the  
example in Agile Web D. with Rails.

I have set up my model like this:

class Entry \< ActiveRecord::Base  
belongs\_to :customer  
belongs\_to :ticket  
belongs\_to :employee  
has\_many :attachments

validates\_presence\_of :body  
end

class IncomingPost \< Entry  
end

class OutgoingPost \< Entry  
end

class PrivatePost \< Entry  
end

class IncomingCall \< Entry  
end

class OutgoingCall \< Entry  
end

I can’t instantiate any of the subclasses. If I load the console here  
is what I get:

> > o = OutgoingPost.new  
> > NameError: uninitialized constant OutgoingPost  
> > from C:/Users/errol.SERENGETI/InstantRails/ruby/lib/ruby/gems/  
> > 1.8/gems/a  
> > ctivesupport-1.4.2/lib/active\_support/dependencies.rb:266:in  
> > `load_missing_const ant' from C:/Users/errol.SERENGETI/InstantRails/ruby/lib/ruby/gems/ 1.8/gems/a ctivesupport-1.4.2/lib/active_support/dependencies.rb:452:in`const\_missing’  
> > from C:/Users/errol.SERENGETI/InstantRails/ruby/lib/ruby/gems/  
> > 1.8/gems/a  
> > ctivesupport-1.4.2/lib/active\_support/dependencies.rb:464:in  
> > `const\_missing’  
> > from (irb):1
> 
> >

It only works if I do something (anything) involving the base Entry  
class (even if it fails). For example, from the console:

> > Entry.find(nil)  
> > ActiveRecord::RecordNotFound: Couldn’t find Entry without an ID  
> > from C:/Users/errol.SERENGETI/InstantRails/ruby/lib/ruby/gems/  
> > 1.8/gems/a  
> > ctiverecord-1.15.3/lib/active\_record/base.rb:1012:in `find_from_ids' from C:/Users/errol.SERENGETI/InstantRails/ruby/lib/ruby/gems/ 1.8/gems/a ctiverecord-1.15.3/lib/active_record/base.rb:419:in`find’  
> > from (irb):1
> 
> > o = OutgoingPost.new  
> > =\> #\<OutgoingPost:0x55b363c @attributes={“body”=\>nil,  
> > “type”=\>“OutgoingPost”, “e  
> > mployee\_id”=\>nil, “customer\_id”=\>nil, “ticket\_id”=\>nil,  
> > “created\_at”=\>nil}, @new  
> > \_record=true\>
> 
> >

This seems like a pretty messy way to do things. Has anybody else  
experienced this?

---

<div class="post-metadata">

**Author:** ![Serengeti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e495f1/32.png) [@Serengeti](https://www.ruby-forum.com/u/Serengeti)\
**Post date:** [June 26, 2007, 6:26am UTC](https://www.ruby-forum.com/t/single-table-inheritance/107739/2 "2007-06-26T06:26:20Z")

</div>

Serengeti wrote:

> end
> 
> > > o = OutgoingPost.new  
> > > NameError: uninitialized constant OutgoingPost

You have to put the subclasses in separate files in the models  
directory: outgoing\_call.rb, etc.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Serengeti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e495f1/32.png) [@Serengeti](https://www.ruby-forum.com/u/Serengeti)\
**Post date:** [June 26, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/single-table-inheritance/107739/3 "2007-06-26T17:23:50Z")

</div>

Thanks – that did it.
