Forum: Ruby on Rails Changing the instantiated table when using STI

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.
Scott B. (Guest)
on 2006-01-25 07:42
(Received via mailing list)
Is there any way to get AR to instantiate a different class when using
STI?  Where it would normally instantiate Digit, I would like it to
instantiate SkelDigit.  Here's why...


My rails app has the following STI hierarchy:

    Digit < Glyph < ActiveRecord::Base

My migrate script can't use those classes directly however (backward
compatibility issues), so I created some skeleton classes:

     SkelDigit < SkelGlyph < ActiveRecord::Base

This way, my migrate script can use the skelton classes to move the
data.

The problem is, the type field of the glyphs table contains "Digit",
"Letter" etc.  Therefore, when I call SkelDigit.find(:all), I receive an
array of Digit objects, not SkelDigit objects.  Then, of course,
everything breaks.

Is there any way I can tell AR to instantiate a SkelDigit instead of a
Digit?  It looks like AR::Base::instantiate has something to do with
this but it's a private routine and poorly documented.

Thank you very much,

    - Scott
This topic is locked and can not be replied to.