Hi all, I was wondering whether anyone could guide me to docu and code examples how to create and use a has_and_belongs_to_many relationship with a joining table where TWO columns are used to identify the relation to TWO alternative tables. Example: table 1: mothers [mother_id, name] table 2: fathers [father_id, name] table 3: children [child_id, name] joining table 4: children_parent [child_id, parent_id, parent_type] ''parent type'' can be either ''mother'' or ''father'', and depending on this, the ''parent_id'' links to an entry in either the father or mother table. How do you define this in Rails and how do you query it when showing the child data? Thanks for any suggestions, Flexer. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Before you get too far, I''d reconsider the models you''re using... Mothers, Fathers, and Children are all people, and ideally a Person is in your database just once (the person who is a child can also be a father, etc). What makes a Person a Mother, or Father (or grandmother, grandfather, etc) is just a relationship between that Person and another. Factor the relationships out into something like: Relation person_1 relation_type person_2 where "person_1" is the "relation_type" of "person_2" and you can start supporting Mother, Father, Sister, Brother, Friend, Enemy, etc, etc. -- 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 -~----------~----~----~----~------~----~------~--~---
Sounds like you want Polymorphic Associations: see http://wiki.rubyonrails.org/rails/pages/HowToUsePolymorphicAssociations On May 27, 6:05 am, meisterflexer <meisterfle...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hi all, > > I was wondering whether anyone could guide me to docu and code > examples how to create and use a has_and_belongs_to_many relationship > with a joining table where TWO columns are used to identify the > relation to TWO alternative tables. > > Example: > table 1: mothers [mother_id, name] > table 2: fathers [father_id, name] > table 3: children [child_id, name] > joining table 4: children_parent [child_id, parent_id, parent_type] > > ''parent type'' can be either ''mother'' or ''father'', and depending on > this, the ''parent_id'' links to an entry in either the father or mother > table. > How do you define this in Rails and how do you query it when showing > the child data? > > Thanks for any suggestions, > Flexer.--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---