shgerstacker-IL5nuyOJLrY@public.gmane.org
2005-Apr-27 13:11 UTC
Problems with Database Relationships
I have a set of items. Each item can have many subitems and each subitem can have many parent items. The thing is, all items are considered the same, so they are all stored in one table. I''m not entirely sure how to map this for RoR. Right now I have an ''items'' table and an ''items_subitems'' table. The ''items_subitems'' table has an item_id and a subitem_id, but technically these are the same. How would I setup a relationship like this? Thanks, Stephen Gerstacker *********************************************************************** The information contained in this transmission is confidential. It is intended solely for the use of the individual(s) or organization(s) to whom it is addressed. Any disclosure, copying or further distribution is not permitted unless such privilege is explicitly granted in writing by JLG Industries, Inc. Further, JLG Industries, Inc. is not responsible for the proper and complete transmission of the substance of this communication nor for any delay in its receipt. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 4/27/05, shgerstacker-IL5nuyOJLrY@public.gmane.org <shgerstacker-IL5nuyOJLrY@public.gmane.org> wrote:> > I have a set of items. Each item can have many subitems and each subitem > can have many parent items. The thing is, all items are considered the > same, so they are all stored in one table. I''m not entirely sure how to map > this for RoR. Right now I have an ''items'' table and an ''items_subitems'' > table. The ''items_subitems'' table has an item_id and a subitem_id, but > technically these are the same. How would I setup a relationship like this? > > Thanks, > Stephen Gerstacker > > *********************************************************************** > The information contained in this transmission is confidential. It is > intended solely for the use of the individual(s) or organization(s) to whom > it is > addressed. Any disclosure, copying or further distribution is not > permitted unless such privilege is explicitly granted in writing by JLG > Industries, Inc. > Further, JLG Industries, Inc. is not responsible for the proper and > complete transmission of the > substance of this communication nor for any delay in its receipt. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >Only use one table. Look at the ActiveRecord method acts_as_tree. [1] Jason [1] http://ar.rubyonrails.com
shgerstacker-IL5nuyOJLrY@public.gmane.org
2005-Apr-27 14:06 UTC
Re: Problems with Database Relationships
That kind of helps, but I do have the case that the child can have multiple parents, which isn''t supported by acts_as_tree. The basic idea is that I have items like a Desktop or Laptop and subitems like a Keyboard, Mouse, Software, etc... You can either get a keyboard by itself, or select to add one when you get a desktop. Therefore, a keyboard is parent item, but it can also be the child of multiple items. I can kludge something together in my .Net/Java world, but I''m having problems setting up this metaphor in RoR. Thanks, Stephen Gerstacker threeve.org-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Sent by: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org 04/27/2005 09:33 AM Please respond to threeve.org; Please respond to rails To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org cc: Subject: Re: [Rails] Problems with Database Relationships On 4/27/05, shgerstacker-IL5nuyOJLrY@public.gmane.org <shgerstacker-IL5nuyOJLrY@public.gmane.org> wrote:> > I have a set of items. Each item can have many subitems and eachsubitem> can have many parent items. The thing is, all items are considered the > same, so they are all stored in one table. I''m not entirely sure how tomap> this for RoR. Right now I have an ''items'' table and an ''items_subitems'' > table. The ''items_subitems'' table has an item_id and a subitem_id, but > technically these are the same. How would I setup a relationship likethis?> > Thanks, > Stephen Gerstacker > > *********************************************************************** > The information contained in this transmission is confidential. It is > intended solely for the use of the individual(s) or organization(s) towhom> it is > addressed. Any disclosure, copying or further distribution is not > permitted unless such privilege is explicitly granted in writing by JLG > Industries, Inc. > Further, JLG Industries, Inc. is not responsible for the proper and > complete transmission of the > substance of this communication nor for any delay in its receipt. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >Only use one table. Look at the ActiveRecord method acts_as_tree. [1] Jason [1] http://ar.rubyonrails.com _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails *********************************************************************** The information contained in this transmission is confidential. It is intended solely for the use of the individual(s) or organization(s) to whom it is addressed. Any disclosure, copying or further distribution is not permitted unless such privilege is explicitly granted in writing by JLG Industries, Inc. Further, JLG Industries, Inc. is not responsible for the proper and complete transmission of the substance of this communication nor for any delay in its receipt. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I think this is about what you want: class Item < ... has_and_belongs_to_many :parents, :class_name => "Item", :join_table => "items_subitems", :association_foreign_key => "parent_id" has_and_belongs_to_many :subitems, :class_name => "Item", :join_table => "items_subitems", :foreign_key => "parent_id" end Then create the join table for the association: create table items_subitems ( item_id int not null, parent_id int not null ); HTH, //jarkko On 27.4.2005, at 17:06, shgerstacker-IL5nuyOJLrY@public.gmane.org wrote:> > That kind of helps, but I do have the case that the child can have > multiple parents, which isn''t supported by acts_as_tree. > > The basic idea is that I have items like a Desktop or Laptop and > subitems like a Keyboard, Mouse, Software, etc... > > You can either get a keyboard by itself, or select to add one when you > get a desktop. Therefore, a keyboard is parent item, but it can also > be the child of multiple items. I can kludge something together in my > .Net/Java world, but I''m having problems setting up this metaphor in > RoR. > > Thanks, > Stephen Gerstacker > > > > threeve.org-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > Sent by: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > 04/27/2005 09:33 AM > Please respond to threeve.org; Please respond to rails > > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > cc: > Subject: Re: [Rails] Problems with Database > Relationships > > > > On 4/27/05, shgerstacker-IL5nuyOJLrY@public.gmane.org <shgerstacker-IL5nuyOJLrY@public.gmane.org> wrote: > > > > I have a set of items. Each item can have many subitems and each > subitem > > can have many parent items. The thing is, all items are considered > the > > same, so they are all stored in one table. I''m not entirely sure > how to map > > this for RoR. Right now I have an ''items'' table and an > ''items_subitems'' > > table. The ''items_subitems'' table has an item_id and a subitem_id, > but > > technically these are the same. How would I setup a relationship > like this? > > > > Thanks, > > Stephen Gerstacker > > > > > *********************************************************************** > > The information contained in this transmission is confidential. It > is > > intended solely for the use of the individual(s) or organization(s) > to whom > > it is > > addressed. Any disclosure, copying or further distribution is not > > permitted unless such privilege is explicitly granted in writing by > JLG > > Industries, Inc. > > Further, JLG Industries, Inc. is not responsible for the proper and > > complete transmission of the > > substance of this communication nor for any delay in its receipt. > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > Only use one table. Look at the ActiveRecord method acts_as_tree. [1] > > > Jason > > [1] http://ar.rubyonrails.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > *********************************************************************** > The information contained in this transmission is confidential. It is > intended solely for the use of the individual(s) or organization(s) to > whom it is > addressed. Any disclosure, copying or further distribution is not > permitted unless such privilege is explicitly granted in writing by > JLG Industries, Inc. > Further, JLG Industries, Inc. is not responsible for the proper and > complete transmission of the > substance of this communication nor for any delay in its receipt. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails