josh.alhani-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-21 17:12 UTC
Two relationships to same table, which way to go?
Feedback here would be excellent! Below are two options for creating a schema where one table has two relationships to the same table in different roles. Please let me know thoughts or any additional options to consider: OPTION 1 create_table :users do |t| t.column :name, :string end create_table :links do |t| t.column :url t.column :submitter_id, :integer t.column :receiver_id, :integer end class User < ActiveRecord::Base has_many :submitted_links, :class_name => ''Link'', :foreign_key => ''submitter_id'' has_many :received_links, :class_name => ''Link'', :foreign_key => ''receiver_id'' end class Link < ActiveRecord::Base belongs_to :submitter, :class_name => ''User'', :foreign_key => ''submitter_id'' belongs_to :receiver, :class_name => ''User'', :foreign_key => ''receiver_id'' end ** CONCERN: Doesn''t this break most of the automagic rails goodness? OPTION 2 create_table :users do |t| t.column :name, :string end create_table :submitters do |t| t.column :user_id, :integer end create_table :receivers do |t| t.column :user_id, :integer end create_table :links do |t| t.column :url t.column :submitter_id, :integer t.column :receiver_id, :integer end class User < ActiveRecord::Base has_one :submitter has_one :receiver end class Submitter < ActiveRecord::Base belongs_to :user has_many :links end class Receiver < ActiveRecord::Base belongs_to :user has_many :links end class Link < ActiveRecord::Base belongs_to :submitter belongs_to :receiver end ** I think this is logically more sound. Opinions? Any thoughts on ** the differences as far as database performance? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
#1. a user can submit or receive. they are still a user. trying to break their role at a moment in time out into an enter new class seems like giant overkill. josh.alhani-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Feedback here would be excellent! Below are two options for creating > a > schema where one table has two relationships to the same table in > different roles. Please let me know thoughts or any additional > options > to consider: > > OPTION 1 > > create_table :users do |t| > t.column :name, :string > end > > create_table :links do |t| > t.column :url > t.column :submitter_id, :integer > t.column :receiver_id, :integer > end > > class User < ActiveRecord::Base > has_many :submitted_links, :class_name => ''Link'', :foreign_key => > ''submitter_id'' > has_many :received_links, :class_name => ''Link'', :foreign_key => > ''receiver_id'' > end > > class Link < ActiveRecord::Base > belongs_to :submitter, :class_name => ''User'', :foreign_key => > ''submitter_id'' > belongs_to :receiver, :class_name => ''User'', :foreign_key => > ''receiver_id'' > end > > ** CONCERN: Doesn''t this break most of the automagic rails goodness? > > OPTION 2 > > create_table :users do |t| > t.column :name, :string > end > > create_table :submitters do |t| > t.column :user_id, :integer > end > > create_table :receivers do |t| > t.column :user_id, :integer > end > > create_table :links do |t| > t.column :url > t.column :submitter_id, :integer > t.column :receiver_id, :integer > end > > class User < ActiveRecord::Base > has_one :submitter > has_one :receiver > end > > class Submitter < ActiveRecord::Base > belongs_to :user > has_many :links > end > > class Receiver < ActiveRecord::Base > belongs_to :user > has_many :links > end > > class Link < ActiveRecord::Base > belongs_to :submitter > belongs_to :receiver > end > > ** I think this is logically more sound. Opinions? Any thoughts on > ** the differences as far as database performance? > > > --~--~---------~--~----~------------~-------~--~----~ > 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 > -~----------~----~----~----~------~----~------~--~--- > >