# Polymorphic associations and STI

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations-and-sti/181681>\
**Category:** Rails\
**Created:** [January 15, 2010, 11:06pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-sti/181681 "2010-01-15T23:06:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f6c823/32.png) [@xinu](https://www.ruby-forum.com/u/xinu)\
**Post date:** [January 15, 2010, 11:06pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-sti/181681/1 "2010-01-15T23:06:54Z")

</div>

If a Problem with a polymorphic association and STI.

class Message \< ActiveRecord::Base  
belongs\_to :source, :polymorphic =\> true  
end

class Letter \< Message  
end

class Post \< ActiveRecord::Base  
has\_many :messages, :as =\> :source  
end

Post.first.messages works fine  
Post.first.letters

Throws “NoMethodError: undefined method `letters’ for  
#Post:0xb6671ef0”

Thanks for your help.

lg  
xinu

---

<div class="post-metadata">

**Author:** ![xinu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f6c823/32.png) [@xinu](https://www.ruby-forum.com/u/xinu)\
**Post date:** [January 16, 2010, 12:28pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-sti/181681/2 "2010-01-16T12:28:44Z")

</div>

Hello

If I’m not completely wrong what you want is just STI:

class Message \< ActiveRecord::Base  
belongs\_to :post  
end

class Letter \< Message  
end

class Post \< ActiveRecord::Base  
has\_many :messages  
has\_many :letters  
end

For each inherited model of Message that you access by  
Post.inherited\_modelname(s) (like in Post.first.letters) you need a  
has\_many declaration in the Post class.

Regards, T.

---

<div class="post-metadata">

**Author:** ![xinu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f6c823/32.png) [@xinu](https://www.ruby-forum.com/u/xinu)\
**Post date:** [January 16, 2010, 5:21pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-sti/181681/3 "2010-01-16T17:21:33Z")

</div>

> For each inherited model of Message that you access by  
> Post.inherited\_modelname(s) (like in Post.first.letters) you need a  
> has\_many declaration in the Post class.

Thanks. I thought has\_many creates automatically the relation to the  
subclasses.

---

<div class="post-metadata">

**Author:** ![xinu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f6c823/32.png) [@xinu](https://www.ruby-forum.com/u/xinu)\
**Post date:** [January 16, 2010, 8:38pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-sti/181681/4 "2010-01-16T20:38:35Z")

</div>

On Jan 16, 11:21Â am, “Michael M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > For each inherited model of Message that you access by  
> > Post.inherited\_modelname(s) (like in Post.first.letters) you need a  
> > has\_many declaration in the Post class.
> 
> Thanks. I thought has\_many creates automatically the relation to the  
> subclasses.

Nope. But note that if you have a post that’s associated to both  
Letters and Messages, you’ll still get both out of the @post.messages  
association.

–Matt J.
