# Object class with STI and Polymorphic Assoc's

**URL:** <https://www.ruby-forum.com/t/object-class-with-sti-and-polymorphic-assocs/60493>\
**Category:** Rails\
**Created:** [May 14, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/object-class-with-sti-and-polymorphic-assocs/60493 "2006-05-14T20:05:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 14, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/object-class-with-sti-and-polymorphic-assocs/60493/1 "2006-05-14T20:05:22Z")

</div>

Hi, lets say I have a a table, “Linkings” that I am using for  
polymorphic associations. One of the associations is to the “person”  
table, and there are several other associations.

“Person” has 2 types, “slacker” and “suckup”.

It is modelled as such:  
class Linking \< ActiveRecord::Base  
belongs\_to :person  
belongs\_to :linkable, :polymorphic =\> true  
end

class Person \< ActiveRecord::Base  
has\_many :linkables  
end  
class Slacker \< Person  
end  
class Yadda \< Person  
end

I’d like to have one controller for both “slacker” and “suckup”, so I  
tried using this:  
@linkable.class

If the linkable type is a “Person” I get either “slacker” or “suckup”,  
the type of “person”. Is there a way to get the actual linkable type?

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 15, 2006, 8:01am UTC](https://www.ruby-forum.com/t/object-class-with-sti-and-polymorphic-assocs/60493/2 "2006-05-15T08:01:21Z")

</div>

Joe C. wrote:

> Hi, lets say I have a a table, “Linkings” that I am using for  
> polymorphic associations. One of the associations is to the “person”  
> table, and there are several other associations.
> 
> “Person” has 2 types, “slacker” and “suckup”.
> 
> It is modelled as such:  
> class Linking \< ActiveRecord::Base  
> belongs\_to :person  
> belongs\_to :linkable, :polymorphic =\> true  
> end
> 
> class Person \< ActiveRecord::Base  
> has\_many :linkables  
> end  
> class Slacker \< Person  
> end  
> class Yadda \< Person  
> end
> 
> I’d like to have one controller for both “slacker” and “suckup”, so I  
> tried using this:  
> @linkable.class
> 
> If the linkable type is a “Person” I get either “slacker” or “suckup”,  
> the type of “person”. Is there a way to get the actual linkable type?

I’m not sure exactly what you’re trying to do, since you left out the  
table schemas/migrations. I guess you’re confusing STI and polymorphic  
associations. STI lets you use inheritance to put subclasses in the same  
table as their common superclass. Polymorphic associations use a  
composite key to allow a model to be associated by models in several  
other tables. I don’t think you need to combine the two for what you’re  
doing.

If you’re doing a polymorphic has\_many in Person, it should go like  
has\_many :linkings, :as =\> :linkable  
But you don’t need to do that since the `belongs_to :person` declaration  
works for slackers and yaddas too.

Drop the polymorphism, just use STI. Does that help?

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

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 15, 2006, 9:36am UTC](https://www.ruby-forum.com/t/object-class-with-sti-and-polymorphic-assocs/60493/3 "2006-05-15T09:36:16Z")

</div>

On 15 May 2006, at 08:01, Josh S. wrote:

> > belongs\_to :linkable, :polymorphic =\> true  
> > I’d like to have one controller for both “slacker” and “suckup”, so I  
> > associations. STI lets you use inheritance to put subclasses in the  
> > declaration  
> > works for slackers and yaddas too.
> 
> Drop the polymorphism, just use STI. Does that help?

I believe what he wants is the following:  
He has a table people and a table companies and a table projects for  
example. A person can be linked to a person, or a company, or a  
project. A company can be linked to a company, a person or a project.  
A project can be linked to another project, a company or a person. So  
basically, you can link anything to anything. In this case, I would  
go for a normal many to many relationship (for the self referential  
one, you can use Rails Recipe n° 18), overriding the defaults of the  
habtm relationship to use that one table which holds the links.

Best regards

Peter De Berdt
