Forum: Ruby on Rails Single Table Inheritance

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Serengeti (Guest)
on 2007-06-25 23:31
(Received via mailing list)
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?
Mark Reginald J. (Guest)
on 2007-06-26 08:26
(Received via mailing list)
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.
Serengeti (Guest)
on 2007-06-26 19:23
(Received via mailing list)
Thanks -- that did it.
This topic is locked and can not be replied to.