Adam Madd
2006-Sep-22 22:58 UTC
possible to create polymorphic relationship with STI models?
I would like to associate a ‘product’ model with 3 models inherited from ‘image’: part, action and accessory. I have it working with Has_many :through relating products to images through attachments, I then have to determine the ‘type’ of the STI extension. This means i have to go product.images. While this is ok, i can see it getting tricky when i want to go products.images << Part.create(:name => "test"). (im on edge rails so the << should work) I would like to have the parts, actions or accessories collections available under products eg products.parts. I have the following tables: product - attachment - images, each with their own model. I also have 3 models that inherit from image eg Part < Image. Ive been following the hasmanythrough article ''the other side of polymorphic''. As well as read most of the other articles...but i just dont get how this will work. According to the docs has_many ignores class_name when using :through. "Options for :class_name and :foreign_key are ignored, as the association uses the source reflection." 1. Attachment tables: 2. 3. (polymorphic attempt) 4. create_table "attachments", :force => true do |t| 5. t.column "product_id", :integer 6. t.column "attachable_id", :integer 7. t.column "attachable_type", :string 8. t.column "created_at", :datetime 9. t.column "created_by", :integer 10. end 11. 20. 21. Models: 22. class Product < ActiveRecord::Base 23. has_many :parts, :through => :attachments, 24. :class_name => :parts, 25. :conditions => "attachments.attachable_type = ''Image''" 26. end 27. 28. 29. class Attachment < ActiveRecord::Base 30. 31. belongs_to :product 32. belongs_to :attachable, :polymorphic => true 33. 34. belongs_to :part, :class_name => "Part", 35. :source => :image, 36. :foreign_key => "attachable_id" 37. end 38. 39. class Part < Image 40. has_many :attachments, :as => :attachable 41. has_many :products, :through => :attachments 42. end Through the console i can do the following: 1. Part.find(:first).products.count 2. >> 1 But this fails 1. Product.find(:first).parts 2. >> ActiveRecordHasManyThroughSourceAssociationNotFOundError 3. .. 4. .. 5. .. 6. associations.rb:876: in ''parts'' This is what i need to work and it doesnt. The many models are the ones that can access the shared models..but not the reverse..hmmm. Thats a pain if your shared model Also, i still have no idea how to create the association with polymorphism. Its easy with just :through ! Im going to go back to the :through association as it makes more sense to me. Would love to hear that im really close and just need to change x and y... =) -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Adam Madd
2006-Sep-22 23:23 UTC
Re: possible to create polymorphic relationship with STI mod
As an update: With the assistance of the new features of edge rails i ahve been able to do the follwoing, brilliant ! product = Product.find(:first) product.images << Part.create(:name => "test part") Remembering that part is a STI instance of image. Which means the only thing i really need to do now is create a way to get the parts, accessories or actions associated with a product. Enabling the following to work: product.parts (returns parts from images table) i have an idea that it should be some sort of association extension in the products model..just not sure how to do it. Any ideas ? -- 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 -~----------~----~----~----~------~----~------~--~---
Adam Madd
2006-Sep-23 00:18 UTC
Re: possible to create polymorphic relationship with STI mod
GOT IT !! it was just this... finally ! class Product < ActiveRecord::Base # through #products - attachments - images has_many :attachments has_many :parts, :through => :attachments, :source => :image, :conditions => "images.type =''Part''" has_many :accessories, :through => :attachments, :source => :image, :conditions => "images.type =''Accessory''" has_many :actions, :through => :attachments, :source => :image, :conditions => "images.type =''Action''" -- 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 -~----------~----~----~----~------~----~------~--~---