# Polymorphic associations and single table inheritance

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations-and-single-table-inheritance/100638>\
**Category:** Rails\
**Created:** [July 31, 2006, 4:04am UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-single-table-inheritance/100638 "2006-07-31T04:04:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rich\_Cavanaugh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d6d6ee/32.png) [@Rich\_Cavanaugh](https://www.ruby-forum.com/u/Rich_Cavanaugh)\
**Post date:** [July 31, 2006, 4:04am UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-single-table-inheritance/100638/1 "2006-07-31T04:04:06Z")

</div>

Running into an interesting problem that at first glance appears to be a  
bug in the AR association code. However, it’s explicitly coded this way  
so I’m not so sure.

I have some code like this:

class Address \< ActiveRecord::Base  
belongs\_to :addressable, :polymorphic =\> true  
end

class Person \< ActiveRecord::Base  
has\_many :addresses, :as =\> :addressable, :dependent =\> :destroy  
end

class Member \< Person  
end

Adding an Address to a Member results in the addressable\_type column  
equaling “Person” when it should be “Member”. Looking in  
ActiveRecord::Associations::AssociationProxy#set\_belongs\_to\_association\_for  
and ActiveRecord::Associations:: HasManyAssociation#construct\_sql it’s  
clear why.

From  
ActiveRecord::Associations::AssociationProxy#set\_belongs\_to\_association\_for:  
record["#{@reflection.options[:as]}\_type"] =  
@owner.class.base\_class.name.to\_s

Removing the call to base\_class results in the behavior I expected. I  
did this by patching the two classes at runtime from a file in lib/.

I guess my question is, are my expectations wrong here or is this a bug?  
The inclusion of base\_class strikes me as something someone meant to do.

Thanks,  
Rich Cavanaugh

---

<div class="post-metadata">

**Author:** ![Rich\_Cavanaugh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d6d6ee/32.png) [@Rich\_Cavanaugh](https://www.ruby-forum.com/u/Rich_Cavanaugh)\
**Post date:** [July 31, 2006, 6:23am UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-single-table-inheritance/100638/2 "2006-07-31T06:23:42Z")

</div>

On Monday, July 31, 2006, at 4:04 AM, Rich Cavanaugh wrote:

> class Person \< ActiveRecord::Base  
> on\_for  
> did this by patching the two classes at runtime from a file in lib/.  
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I ran into this too. I also noted that if you manually change the  
‘type’ column to match the subclass name, things seem to work fine.

It does really mess with some functionality in acts\_as\_taggable if your  
tagged object is STI. It’s probably worth running the core AR tests  
against your patched version to see if anything breaks.

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![Rich\_Cavanaugh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d6d6ee/32.png) [@Rich\_Cavanaugh](https://www.ruby-forum.com/u/Rich_Cavanaugh)\
**Post date:** [July 31, 2006, 7:01am UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-single-table-inheritance/100638/3 "2006-07-31T07:01:30Z")

</div>

On Monday, July 31, 2006, at 12:53 AM, bacchusrx wrote:

> There’s more to the change than simply assigning the real class name  
> and I’ve been meaning to make a patch, but I haven’t gotten the chance  
> yet. There’s probably some reason for the way things are, but it’s  
> eluded me thus far 😉
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

It might be worth inquiring on the rails core list about this.

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![Rich\_Cavanaugh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d6d6ee/32.png) [@Rich\_Cavanaugh](https://www.ruby-forum.com/u/Rich_Cavanaugh)\
**Post date:** [July 31, 2006, 6:55am UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-single-table-inheritance/100638/4 "2006-07-31T06:55:09Z")

</div>

On 31 Jul 2006 04:25:44 -0000, Kevin O.:

> I ran into this too. I also noted that if you manually change the  
> ‘type’ column to match the subclass name, things seem to work fine.
> 
> It does really mess with some functionality in acts\_as\_taggable if your  
> tagged object is STI. It’s probably worth running the core AR tests  
> against your patched version to see if anything breaks.

I’ve also had to make similar changes while working on a recent app.

There’s more to the change than simply assigning the real class name  
instead of the base class name.

You also have to change the SQL generated by, eg,  
ActiveRecord::Assocations#association\_join. Otherwise, using your  
associations as includes in find() won’t work anymore. Instead, the  
join condition should look like the SQL generated by  
ActiveRecord::Base#type\_condition. If you simply use the real class  
name there, subclasses of your subclass won’t be included.

The modifications to AR that I’m using pass all the existing tests,  
and I’ve been meaning to make a patch, but I haven’t gotten the chance  
yet. There’s probably some reason for the way things are, but it’s  
eluded me thus far 😉
