I have doubts on Polymorphic Assosiations. On Which situations we are using these?? What are the advantages? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 Aug 2010, at 13:37, Sateesh Kambhamapati wrote:> I have doubts on Polymorphic Assosiations. > On Which situations we are using these??I used these recently for a pinboard. We have an internal system that contains contact management, support tickets, products & services, billing etc. These are all different represented by different active record models. Users wanted to be able to pin arbitrary objects to an area of the site/app that would be always available. For example the could have a couple of support tickets, a contact record and maybe a few service records all accessible in the same left hand div anywhere in the app. I did this with the following model: <snip> class PinnedItem < ActiveRecord::Base belongs_to :user belongs_to :pinned, :polymorphic => true validates :pinned_id, :uniqueness => {:scope => [:pinned_type, :user_id]} if User.current default_scope where(:user_id => User.current.id) end end </snip>> What are the advantages?I couldn''t say. This just seemed like the easiest way to accomplish this, that is having a normal AR association but with arbitrary rather than a specific class of objects. This is the first time I''ve used polymorphic associations so if there any major disadvantages that anyone''s aware of I''d like to know. Regards, Jim -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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?hl=en.
Sateesh Kambhamapati wrote:> I have doubts on Polymorphic Assosiations. > On Which situations we are using these?? > What are the advantages?I use polymorphic associations all over the place in one application... mostly for the reporting and research aspect of the application. A ''functional_area'' model can be related to a project, a testing scenario, a specific unit test, a functional requirement, or a marketing requirement. With polymorphic links, I have 1 arealink table that can link a functional area to any of those other models. The same logic applies to ''modules'', ''applications'', ''features'', ''functional_requirements'', etc. The initial ERD I received was almost unreadable and looked like a badly done Spirograph drawing (does anyone else know what those are anymore?) When I switched to polymorphic associations, I eliminated 18 separate join tables from the design in favor of several polymorphic join tables and was able to create 1 ''linking'' partial (about 30 lines of haml) to manage creating/removing links no matter which polymorphic association was being altered from a source model to any type of target model. When I have the time, I''m going to look into a double-polymorphic join table and get those join tables down to 1. The only downside I''ve seen thus far is that I have to restart my server when I add polymorphic join definitions, but that''s no big deal. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.