# Is this where you could use polymorphic associations?

**URL:** <https://www.ruby-forum.com/t/is-this-where-you-could-use-polymorphic-associations/143939>\
**Category:** Rails\
**Created:** [July 24, 2008, 12:31am UTC](https://www.ruby-forum.com/t/is-this-where-you-could-use-polymorphic-associations/143939 "2008-07-24T00:31:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliotchyba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8dc957/32.png) [@elliotchyba](https://www.ruby-forum.com/u/elliotchyba)\
**Post date:** [July 24, 2008, 12:31am UTC](https://www.ruby-forum.com/t/is-this-where-you-could-use-polymorphic-associations/143939/1 "2008-07-24T00:31:28Z")

</div>

Could somebody help me wrap my head around this one?

Tables: users  
id  
username  
password

Tables: friends  
id  
user\_id  
share\_id

Tables: books  
id  
user\_id  
title  
author

Users get to store as many books as they want.  
Users has\_many :books, and books belong\_to :user  
Users have the option of adding other users so that they can view their  
books to.

For this process, a new record is created in Friends with the user\_id of  
the current user and share\_id of the user they want to view.

How do you perform a find on the books of a user’s friends? I could use  
a through association on searching the books that belong just to the  
user. A loop would be terribly inefficient. After reading about  
polymorphic associations, it seemed like an option but I just can’t  
figure it out.

Thanks in advance to anyone who can lend a hand.
