# Polymorphic Association with Single Table Inheritance?

**URL:** <https://www.ruby-forum.com/t/polymorphic-association-with-single-table-inheritance/100061>\
**Category:** Rails\
**Created:** [July 26, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/polymorphic-association-with-single-table-inheritance/100061 "2006-07-26T22:54:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bcef8e/32.png) [@Stephan\_S](https://www.ruby-forum.com/u/Stephan_S)\
**Post date:** [July 26, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/polymorphic-association-with-single-table-inheritance/100061/1 "2006-07-26T22:54:34Z")

</div>

Hello,

is it possible to setup a model/table schema like this:

Groupable --\> Membership \<-- Group  
^ ^  
| |  
User UserGroup

I tried the following but failed:

Groupable (table with ‘type’ column)  
has\_many :memberships, :as =\> :groupable  
has\_many :groups, :through =\> :memberships

Membership  
belongs\_to :groupable, :polymorphic =\> true  
belongs\_to :group, :polymorphic =\> true

Group (table with ‘type’ column)  
has\_many :memberships, :as =\> :group  
has\_many :groupables, :through =\> :memberships

And the inherited tables:

User \< Groupable  
has\_many :user\_groups, :through =\> :memberships

UserGroup \< Group  
has\_many :users, :through =\> :memberships

Setting up an user, a membership and an user\_group  
works, but it throws an exception on accessing  
user\_group.users:

> > ruby script/console  
> > u = User.create  
> > m = Membership.create  
> > ug = UserGroup.create  
> > u.memberships \<\< m  
> > ug.memberships \<\< m  
> > ug.users  
> > ActiveRecord::HasManyThroughAssociationPolymorphicError:  
> > ActiveRecord::HasManyThroughAssociationPolymorphicError

Thanks for any help!

Greets,  
Stephan

---

<div class="post-metadata">

**Author:** ![Stephan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bcef8e/32.png) [@Stephan\_S](https://www.ruby-forum.com/u/Stephan_S)\
**Post date:** [July 27, 2006, 4:38am UTC](https://www.ruby-forum.com/t/polymorphic-association-with-single-table-inheritance/100061/2 "2006-07-27T04:38:37Z")

</div>

Stephan S. wrote:

> Hello,
> 
> is it possible to setup a model/table schema like this:
> 
> Groupable → Membership ← Group  
> ^ ^  
> | |  
> User UserGroup
> 
> I tried the following but failed:
> 
> Groupable (table with ‘type’ column)  
> has\_many :memberships, :as =\> :groupable  
> has\_many :groups, :through =\> :memberships
> 
> Membership  
> belongs\_to :groupable, :polymorphic =\> true  
> belongs\_to :group, :polymorphic =\> true
> 
> Group (table with ‘type’ column)  
> has\_many :memberships, :as =\> :group  
> has\_many :groupables, :through =\> :memberships
> 
> And the inherited tables:
> 
> User \< Groupable  
> has\_many :user\_groups, :through =\> :memberships
> 
> UserGroup \< Group  
> has\_many :users, :through =\> :memberships
> 
> Setting up an user, a membership and an user\_group  
> works, but it throws an exception on accessing  
> user\_group.users:
> 
> > > ruby script/console  
> > > u = User.create  
> > > m = Membership.create  
> > > ug = UserGroup.create  
> > > u.memberships \<\< m  
> > > ug.memberships \<\< m  
> > > ug.users  
> > > ActiveRecord::HasManyThroughAssociationPolymorphicError:  
> > > ActiveRecord::HasManyThroughAssociationPolymorphicError

has\_many :through works just fine with STI, however it has some issues  
with polymorphism. I have an explanation and a partial workaround here:  
[http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through](http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through)

But it doesn’t look like you are doing STI at all, just garden-variety  
polymorphism. The terminology can be confusing, as generic OOP  
inheritance allows polymorphism through specialization, meaning a  
subclass can be used in place of a superclass. But in Rails STI just  
means the models get their data from the same table, which allows the  
type information to be encapsulated in the model itself (in the ‘type’  
field in the table), rather than being placed alongside the groupable\_id  
foreign key in the groupable\_type field. You only need to have the type  
in one place, so only use STI or :as =\> :typeable.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![Stephan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bcef8e/32.png) [@Stephan\_S](https://www.ruby-forum.com/u/Stephan_S)\
**Post date:** [July 27, 2006, 10:13am UTC](https://www.ruby-forum.com/t/polymorphic-association-with-single-table-inheritance/100061/3 "2006-07-27T10:13:49Z")

</div>

Josh S. wrote:

> But it doesn’t look like you are doing STI at all, just garden-variety  
> polymorphism.

Ah, got the point now. A table containing all attributes of all possible  
‘subclasses’…

Thank you, read all your blog entries about rails - pretty good stuff 🙂

Greets,  
Stephan
