Single Table Inheritance

I am trying to implement single table inheritance according to the example in Agile Web Development 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?

Serengeti wrote:

I am trying to implement single table inheritance according to the example in Agile Web Development 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

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

Thanks -- that did it.