# Two relationships to same table, which way to go?

**URL:** <https://www.ruby-forum.com/t/two-relationships-to-same-table-which-way-to-go/104706>\
**Category:** Rails\
**Created:** [May 21, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/two-relationships-to-same-table-which-way-to-go/104706 "2007-05-21T19:39:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 21, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/two-relationships-to-same-table-which-way-to-go/104706/1 "2007-05-21T19:39:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 22, 2007, 12:53am UTC](https://www.ruby-forum.com/t/two-relationships-to-same-table-which-way-to-go/104706/2 "2007-05-22T00:53:34Z")

</div>

#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.
