# Multi polymorphic relationship?

**URL:** <https://www.ruby-forum.com/t/multi-polymorphic-relationship/132657>\
**Category:** Rails\
**Created:** [March 22, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/multi-polymorphic-relationship/132657 "2008-03-22T18:33:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [March 22, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/multi-polymorphic-relationship/132657/1 "2008-03-22T18:33:28Z")

</div>

In my app, I want to let users mark various relationship between them  
and other users and objects. Example

john selects mary as favorite user  
john selects mary as featured user of the month  
john puts mary in bookmarks  
john selects “funny cat” as favorite picture  
john votes for “funny cat” as featured picture of the month  
john puts “funny cat” in bookmarks

I could have a bookmark model, a favorite model, and a feature model  
and use polymorphic association. But this seems like a lot of  
duplication.

acts\_as\_favorable  
acts\_as\_bookmarkable  
acts\_as\_featureable

I really just want to say something like

acts\_as\_markable :models =\> [:favorite,:bookmark,:feature]

u=User.find\_ny\_name(‘john’)  
p=Picture.find 10  
u.mark\_favorite( p)  
u.mark\_feature( p)  
u2 = User.find\_by\_name(‘mary’)  
u.mark\_bookmark( u2)  
u.marked\_bookmark?( u2) # true  
u.marked\_favorite\_users # list of favorite users

Suggestions?

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [March 22, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/multi-polymorphic-relationship/132657/2 "2008-03-22T20:30:18Z")

</div>

rubynuby wrote:

> I really just want to say something like
> 
> acts\_as\_markable :models =\> [:favorite,:bookmark,:feature]

What you want is the wonderful has\_many\_polymorphs =\>  
[http://blog.evanweaver.com/files/doc/fauna/has\_many\_polymorphs/files/README.html](http://blog.evanweaver.com/files/doc/fauna/has_many_polymorphs/files/README.html)

- Niels.

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [March 23, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/multi-polymorphic-relationship/132657/3 "2008-03-23T18:24:25Z")

</div>

On Mar 22, 12:28 pm, Niels G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > acts\_as\_featureable
> 
> > I really just want to say something like
> 
> > acts\_as\_markable :models =\> [:favorite,:bookmark,:feature]
> 
> What you want is the wonderful has\_many\_polymorphs =\>[http://blog.evanweaver.com/files/doc/fauna/has\_many\_polymorphs/files/](http://blog.evanweaver.com/files/doc/fauna/has_many_polymorphs/files/)…
> 
> - Niels.

Thanks, I read the doc several times but I still don’t get how I can  
use it in my case. It seems I still need a separate join model for  
favorite, bookmark, etc. ??

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [March 24, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/multi-polymorphic-relationship/132657/4 "2008-03-24T15:06:25Z")

</div>

You may be able to use a combination of Single Table Inheritance and  
polymorphic associations. STI would handle distinguishing bookmarks  
from favorites from features, etc while allowing them all to share the  
‘mark’ behavior. Polymporphic associations, as I think you already  
understood, would allow you to ‘mark’ an object of any type.
