Adam Roth
2007-May-11 02:44 UTC
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Devin Ben-Hur
2007-May-11 05:54 UTC
Re: Polymorphic has_many :images, :through => [...] - possible?
On 5/10/07, Adam Roth <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---