Forum: Ruby on Rails Two relationships to same table, which way to go?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2007-05-21 21:39
(Received via mailing list)
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?
Sean T Allen (Guest)
on 2007-05-22 02:53
(Received via mailing list)
#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.
This topic is locked and can not be replied to.