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

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
3fd97b8297aab3006334d746f7c10b3d?d=identicon&s=25 Scott Bronson (Guest)
on 2006-01-25 06: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

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.