# STI question

**URL:** <https://www.ruby-forum.com/t/sti-question/128806>\
**Category:** Rails\
**Created:** [February 11, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/sti-question/128806 "2008-02-11T18:49:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [February 11, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/sti-question/128806/1 "2008-02-11T18:49:16Z")

</div>

Hi,

I am learning Single Table Inheritance and have a question.

In my application, a message may be written by a user or by a guest.  
Thus a writer of a message is virtually associated either to a user or a  
guest.

class Message \< ActiveRecord::Base  
end

class UserMessage \< Message  
belongs\_to :writer, :class\_name =\> ‘User’  
end

class GuestMessage \< Message  
belongs\_to :writer, :class\_name =\> ‘Guest’  
end

When I retrieve a list of messages, I want to include writer association  
for performance.

messages = Message.find :all, :include =\> [:writer] # This doesn’t work.

How can I make the above code work?  
If not, is there an workaround?

Thanks.  
Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [February 12, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/sti-question/128806/2 "2008-02-12T19:28:43Z")

</div>

From what you’re describing it sounds like you might be better with a  
single User model, with guest as a role, rather than separate models.  
In that way Message would have\_one wrter :class\_name=\>‘User’.

On Feb 11, 12:49 pm, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
