# STI / Polymorphic Association Issue (or possible bug)?

**URL:** <https://www.ruby-forum.com/t/sti-polymorphic-association-issue-or-possible-bug/139369>\
**Category:** Rails\
**Created:** [June 3, 2008, 10:09am UTC](https://www.ruby-forum.com/t/sti-polymorphic-association-issue-or-possible-bug/139369 "2008-06-03T10:09:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@Derek\_P](https://www.ruby-forum.com/u/Derek_P)\
**Post date:** [June 3, 2008, 10:09am UTC](https://www.ruby-forum.com/t/sti-polymorphic-association-issue-or-possible-bug/139369/1 "2008-06-03T10:09:15Z")

</div>

Hey everyone,

I have been working on some code lately that seems to be yielding some  
unexpected results. Basically, I have a class that is using single  
table inheritance for basic subclassing, and I wanted the base class  
to be able to use a polymorphic interface for connecting up the  
different classes of information generically using one table.

here is a pastie of the example: [http://pastie.caboo.se/207583](http://pastie.caboo.se/207583)

Am I doing something wrong? Is this a bug? looking for advice.

Thanks!

---

<div class="post-metadata">

**Author:** ![Derek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@Derek\_P](https://www.ruby-forum.com/u/Derek_P)\
**Post date:** [June 3, 2008, 11:17am UTC](https://www.ruby-forum.com/t/sti-polymorphic-association-issue-or-possible-bug/139369/2 "2008-06-03T11:17:52Z")

</div>

- Derek P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2008-06-02 19:07:33 -0700]:

> here is a pastie of the example: [Parked at Loopia](http://pastie.caboo.se/207583)  
> You should put the has\_many in SubPost

> Am I doing something wrong? Is this a bug? looking for advice.
> 
> Thanks!
> 
> >

–  
jan=callcc{|jan|jan};jan.call(jan)

---

<div class="post-metadata">

**Author:** ![Derek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@Derek\_P](https://www.ruby-forum.com/u/Derek_P)\
**Post date:** [June 3, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/sti-polymorphic-association-issue-or-possible-bug/139369/3 "2008-06-03T18:12:18Z")

</div>

Hey Xie,

Actually, it doesn’t matter where the has\_many lives. Due to  
inheritance, it is actually defined for both classes. I spoke with  
bitsweat on IRC about this behavior and it turns out its expected, and  
here is why:

All STI-based interactions with polymorphic objects will refer to the  
base class of the STI, by design. But, if you actually load that  
object into memory and check its class, you’ll see that its actually  
lazy-loaded (via the classable\_id) and becomes the proper class at  
runtime. Now, I don’t _love_ this design, and frankly, it will make it  
much harder to deal with as far as filtering is concerned, but hey, at  
least it works.

Thanks for the help though.

- Derek
