# Polymorphic associations in reverse?

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations-in-reverse/97796>\
**Category:** Rails\
**Created:** [July 10, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-reverse/97796 "2006-07-10T17:19:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seth\_Morabito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@Seth\_Morabito](https://www.ruby-forum.com/u/Seth_Morabito)\
**Post date:** [July 10, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-reverse/97796/1 "2006-07-10T17:19:12Z")

</div>

My app has an association that’s got me scratching my head.

I have Users, and Images. A User has many Images. (Yes, another  
picture sharing app, but it’s for a small niche, and it doesn’t end  
in -r!)

I would like to express a “Favorite” relationship between both Users  
to Users, and Users to Images. This seems like the opposite of the  
standard polymorphic model, i.e. tags and taggable, where a Tag may  
apply to many different types of Thing.

Perhaps I’m not thinking of it properly, but this was my first stab  
at the relationship:

###############################################################  
class Image \< ActiveRecord::Base

# Can be a favorite

belongs\_to :favorite  
belongs\_to :favorable, :polymorphic =\> :true  
end

class User \< ActiveRecord::Base  
has\_many :images

# Can be a favorite

belongs\_to :favorite  
belongs\_to :favorable, :polymorphic =\> :true

# Has many favorite ‘things’

has\_many :favorites, :dependent =\> :destroy  
has\_many :favorite\_images, :as =\> :favorable, :through =\> :favorites,  
:classname =\> “Image”  
has\_many :favorite\_users, :as =\> :favorable, :through =\> :favorites,  
:classname =\> “User”  
end

class Favorite \< ActiveRecord::Base  
belongs\_to :favorable, :polymorphic =\> true  
end

create\_table “favorites” do |t|  
t.column :user\_id, :integer  
t.column :favorable\_id, :integer  
t.column :favorable\_type, :string  
end

create\_table “images” do |t|  
t.column :name, :string  
end

create\_table “users” do |t|  
t.column :name, :string  
end  
###############################################################

This is of course wrong and does not work 🙂 But I don’t know  
whether I’m thinking of the problem incorrectly, or whether there’s  
even a proper way to do this sort of reverse-polymorphic-join in  
ActiveRecord.

Does anyone have any advice?

-Seth

---

<div class="post-metadata">

**Author:** ![Seth\_Morabito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@Seth\_Morabito](https://www.ruby-forum.com/u/Seth_Morabito)\
**Post date:** [July 10, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-reverse/97796/2 "2006-07-10T17:57:37Z")

</div>

The main page on polymorphic associations on the wiki actually is a  
reverse relationship (folder has many textfiles, images, etc, from  
memory). I found it very helpful:  
[http://wiki.rubyonrails.com/rails/pages/HowToUsePolymorphicAssociations](http://wiki.rubyonrails.com/rails/pages/HowToUsePolymorphicAssociations)

---

<div class="post-metadata">

**Author:** ![Seth\_Morabito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@Seth\_Morabito](https://www.ruby-forum.com/u/Seth_Morabito)\
**Post date:** [July 10, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-reverse/97796/3 "2006-07-10T18:00:55Z")

</div>

Seth Morabito wrote:

> My app has an association that’s got me scratching my head.
> 
> I have Users, and Images. A User has many Images. (Yes, another  
> picture sharing app, but it’s for a small niche, and it doesn’t end  
> in -r!)
> 
> I would like to express a “Favorite” relationship between both Users  
> to Users, and Users to Images. This seems like the opposite of the  
> standard polymorphic model, i.e. tags and taggable, where a Tag may  
> apply to many different types of Thing.

Have to say I’d be tempted to split the two types of favourite for  
simplicities sake.

Say, Favourites and Contacts (for example).

A.
