Polymorphic has_many :images, :through => [...] - possible?

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

On 5/10/07, Adam R. [email protected] 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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs