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