So i have one table of data, each data object has two owners from the users table. so it''s data table id owner1_id owner2_id --------------------- 5 1 2 10 1 2 owner table id name --------------- 1 bob 2 cornholio so when i pull records from the data table for owner1_id how would i access the name field of owner id 2? -- 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 -~----------~----~----~----~------~----~------~--~---
You need three tables to represent a many-to-many association. class Owner < ActiveRecord::Base has_and_belongs_to_many :datas end class Data < ActiveRecord::Base has_and_belongs_to_many :owners end Table owners: ----------------------- id: integer name: string Table datas: ----------------------- id: integer value: integer Table datas_owners: ------------------------------ data_id: integer owner_id: integer This is shown using the more basic has_and_belongs_to_many (HABTM) associations. However, in some cases it''s necessary to store additional information in the join table managing your many-to-many association. In which case your can make the join table into model class and use has_many :through. Having owner1_id, owner2_id is in violation of the "First Normal Form (1NF)" relational database rule. http://en.wikipedia.org/wiki/First_normal_form#Repeating_groups On Aug 1, 12:07 am, Morgan Morgan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> So i have one table of data, each data object has two owners from the > users table. > > so it''s > > data table > > id owner1_id owner2_id > --------------------- > 5 1 2 > 10 1 2 > > owner table > > id name > --------------- > 1 bob > 2 cornholio > > so when i pull records from the data table for owner1_id how would i > access the name field of owner id 2? > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Walker wrote:> > Having owner1_id, owner2_id is in violation of the "First Normal Form > (1NF)" relational database rule. > > http://en.wikipedia.org/wiki/First_normal_form#Repeating_groups > > On Aug 1, 12:07�am, Morgan Morgan <rails-mailing-l...@andreas-s.net>the owner1_id corresponds to the student associated with the entry. the owner2_id corresponds to the teacher associated with the entry. i just used the owner1 and 2 for sake of simplicity. there is a table of students and teachers with a user level to decide what they are thats used for basic information and authentication. it seemed more logical to treat it that way.. please correct me if i''m wrong.. i''m a little tarded when it comes to database design. -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---