mazurr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Oct-17 01:35 UTC
Tables, Fields, and Naming Conventions
Is there a way to have a table with two references to the another table using two fields? Here is what I mean. I have two tables, items and billofmaterials: table: ITEMS id itemname itemdesc table: BILLOFMATERIALS id item (which is an item in ITEMS table) component (which is also an item in ITEMS table) qty uom For a single record of BILLOFMATERIALS, both of the fields "item" and "component" are items from the ITEMS table. In my html and such I would like to call the equivalent of: billofmaterials.item.itemname billofmaterials.component.itemname But I can not due to ROR naming conventions. Rather, I need to create my BILLOFMATERIALS table like: BILLOFMATERIALS id item_id (to reference the ITEMS table) component ...thus allowing me to call: billofmaterials.item.itemname But how can I do the same for the "component" field in BILLOFMATERIALS, as I certainly can not have two fields called "item_id". --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The Answer should be Polymorphic assosiations. class Item < ActiveRecord::Base has_many :bill_of_materials, :as => :component has_many :bill_of_materials, :as => :item end class BillOfMaterial < ActiveRecord::Base belongs_to :item, :polymorphic => true belongs_to :component, :polymorphic => true end Yout bill_of_materials table should look like this: id item_id item_type component_id component_type qty uom The *_type fields are required for the polymorphic association and handled my ActiveRecord, you dont have to give any values.... you could now do this: i = Item.find(1) #get an Item c = Item.find(2) #get another Item, this will be the "component" b = BillOfMaterial.new #create a new BillOfMaterial object b.item = i # set this Item as Item of the new Bill... object b.component = c #set this Item as Component in the new Bill... object b.save! i hope i didnt twist up any singular/plural stuff, or any other errors. never tested this polymorphic stuff really, bu i think this should be the way to go. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
mazurr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Oct-17 22:20 UTC
Re: Tables, Fields, and Naming Conventions
Thorsten, (please note that what follows are the actual names of my objects and db tables, compared to my original post....sorry for the confusion....the layout is still the exact same) Thanks so much for your detailed response. I have been knocking away at this since your post, with near-success. I am able to save new bom (billofmaterial) records, and the invitem_id and component_id fields are populating in the db with the appropriate id''s from the INVITEMS table. But I am not able to use (for example, in my basic view for boms): bom.invitem.name I get: You have a nil object when you didn''t expect it! The error occured while evaluating nil.name Merely calling for: bom.invitem_id ...works fine though. Now, in looking around for a cause, I see that invitem_type and component_type in the boms table are populating with NULL. I know you mentioned... "The *_type fields are required for the polymorphic association and handled my ActiveRecord, you dont have to give any values....". But, ROR is probably supposed to put something in those field other than NULL, right? Something to do with the object that is expected in invitem_id and component_id? So I am trying to see where my error in the config is. I have: class Bom < ActiveRecord::Base belongs_to :invitem, :polymorphic => true belongs_to :component, :polymorphic => true end class Invitem < ActiveRecord::Base belongs_to :itemtype belongs_to :vendor has_many :boms, :as => :invitem has_many :boms, :as => :component end mysql> desc boms; +----------------+--------------- | Field | Type +----------------+--------------- | id | int(11) | date_added | date | invitem_id | varchar(20) | invitem_type | varchar(20) | component_id | varchar(20) | component_type | varchar(20) | qty | decimal(10,2) | uom | varchar(5) +----------------+--------------- One question that pops to mind is, in my Bom.rb class, how does ROR know what "belongs_to :component" does? Is that some under-the-cover magic? Should I be expected to have a Component.rb somewhere? I don''t necessarily want one....as "component" is only a field in the boms table...and in reality "is an invitem". Could the fact that I am using "has_many :boms, :as => :invitem" be causing conflict, as I actually have a class called Invitem? I know the part "...:as => :invitem" is supposed to be an arbitrary name. Hmmm? What might I be missing here? Thanks so much for the response. Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---