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?