tisp
2007-Jan-11 11:39 UTC
Two-side-polymorphic association - or: how to link multiple models to each other
Hi, I''m planning an application with a bunch of models which can be linked to each other. Since a few days I''m thinking about the right way to go, to implement these model associations. First thought was, I should use polymorphic associations, but I think it''s not enough for my case. I came up with the idea of an explicit link model which would look like this: LinkModel ---------------------------------------------------- | source_id | source_type | target_id |target_type | There''re a few problems with this kind of association: Every model can be a source OR a target. Let''s say I want to get every company which is linked to a specific person (id=42), I have to do two queries to get the ids for all companies linked to a person. SELECT target_id FROM link_model WHERE source_type = ''person'' AND source_id = ''42'' AND target_type = ''company'' SELECT source_id FROM link_model WHERE target_type = ''person'' AND target_id = ''42'' AND source_type = ''company'' How do I implement this in Rails? This looks a bit like a Two-Side-Polymorphic-Association. Is this possible? Or are there any other ideas how to do this kind of association? tisp --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Chris Hall
2007-Jan-11 16:00 UTC
Re: Two-side-polymorphic association - or: how to link multiple models to each other
i think this might be what you are looking for: http://wiki.rubyonrails.org/rails/pages/ManytoManyPolymorphicAssociations On 1/11/07, tisp <t.springmann-yM2StcZat7YdDX0dnZFiFbNAH6kLmebB@public.gmane.org> wrote:> > Hi, > > I''m planning an application with a bunch of models which can be linked > to each other. Since a few days I''m thinking about the right way to go, > to implement these model associations. First thought was, I should use > polymorphic associations, but I think it''s not enough for my case. I > came up with the idea of an explicit link model which would look like > this: > > LinkModel > ---------------------------------------------------- > | source_id | source_type | target_id |target_type | > > > There''re a few problems with this kind of association: > > Every model can be a source OR a target. Let''s say I want to get every > company which is linked to a specific person (id=42), I have to do two > queries to get the ids for all companies linked to a person. > > SELECT target_id FROM link_model WHERE source_type = ''person'' AND > source_id = ''42'' AND target_type = ''company'' > SELECT source_id FROM link_model WHERE target_type = ''person'' AND > target_id = ''42'' AND source_type = ''company'' > > How do I implement this in Rails? This looks a bit like a > Two-Side-Polymorphic-Association. Is this possible? Or are there any > other ideas how to do this kind of association? > > tisp > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
tisp
2007-Jan-11 18:14 UTC
Re: Two-side-polymorphic association - or: how to link multiple models to each other
Not exactly, but it''s a start. That solution is not truly two-sides, as it only implements one of the above SQL statements. Also it''s not really beautiful as I it could be?! tisp --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---