# Polymorphic has\_many :images, :through =\> \[...\] - possible?

**URL:** <https://www.ruby-forum.com/t/polymorphic-has-many-images-through-possible/103869>\
**Category:** Rails\
**Created:** [May 11, 2007, 4:44am UTC](https://www.ruby-forum.com/t/polymorphic-has-many-images-through-possible/103869 "2007-05-11T04:44:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aroth](https://www.ruby-forum.com/u/aroth)\
**Post date:** [May 11, 2007, 4:44am UTC](https://www.ruby-forum.com/t/polymorphic-has-many-images-through-possible/103869/1 "2007-05-11T04:44:47Z")

</div>

I have a polymorphic image model (profiles have images, products have  
images, user has a profile, user has products). I’d like to be able to  
access all of a user’s images regardless of which model they represent.  
This doesn’t work:

has\_many :images, :through =\> [:profile, :products]

… as :through =\> doesn’t work with an array.

Of course I could add a user\_id column to the Image table, but I wanted  
to see if this concept was possible first.

Any ideas?

# ======== CODE

class User \< ActiveRecord::Base  
has\_one :profile  
has\_many :products

has\_many :images, :through =\> [:profile, :products]  
end

class Profile \< ActiveRecord::Base  
belongs\_to :user  
has\_many :images, :as =\> :imageable  
end

class Product \< ActiveRecord::Base  
belongs\_to :user  
has\_many :images, :as =\> :imageable  
end

# ======== ROUTES

map.resources :profiles do |profile|  
profile.resources :images, :name\_prefix =\> ‘profile\_’  
end

map.resources :products do |product|  
product.resources :images, :name\_prefix =\> ‘product\_’  
end

---

<div class="post-metadata">

**Author:** ![aroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aroth](https://www.ruby-forum.com/u/aroth)\
**Post date:** [May 11, 2007, 7:55am UTC](https://www.ruby-forum.com/t/polymorphic-has-many-images-through-possible/103869/2 "2007-05-11T07:55:26Z")

</div>

On 5/10/07, Adam R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :images, :through =\> [:profile, :products]

Your through should be based on the polymorphic class name, have you  
tried?

has\_many :images, :through =\> :imageable

You may want to try the has\_many\_polymorphs plugin to help you out too.  
[http://rubyforge.org/projects/polymorphs/](http://rubyforge.org/projects/polymorphs/)
