# Multiple user\_id columns

**URL:** <https://www.ruby-forum.com/t/multiple-user-id-columns/48489>\
**Category:** Rails\
**Created:** [November 21, 2005, 10:48am UTC](https://www.ruby-forum.com/t/multiple-user-id-columns/48489 "2005-11-21T10:48:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jjones](https://www.ruby-forum.com/u/jjones)\
**Post date:** [November 21, 2005, 10:48am UTC](https://www.ruby-forum.com/t/multiple-user-id-columns/48489/1 "2005-11-21T10:48:48Z")

</div>

I have a table called ‘users’. I have a second table called ‘comments’.  
The comments table is in need of _two_ columns that both reference  
different user ids within the ‘users’ table (one column for the user id  
that originated the post, another column for the user id for which the  
message is intended for.

Obviously I can’t create two columns named ‘user\_id’. How do I go about  
creating two columns that reference the same foreign column?

TIA.

Jim

---

<div class="post-metadata">

**Author:** ![jjones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jjones](https://www.ruby-forum.com/u/jjones)\
**Post date:** [November 21, 2005, 11:09am UTC](https://www.ruby-forum.com/t/multiple-user-id-columns/48489/2 "2005-11-21T11:09:00Z")

</div>

Jim J. wrote:

> I have a table called ‘users’. I have a second table called ‘comments’.  
> The comments table is in need of _two_ columns that both reference  
> different user ids within the ‘users’ table (one column for the user id  
> that originated the post, another column for the user id for which the  
> message is intended for.
> 
> Obviously I can’t create two columns named ‘user\_id’. How do I go about  
> creating two columns that reference the same foreign column?

class User \< ActiveRecord::Base  
has\_many :sent\_comments, :class\_name =\> ‘Comment’, :conditions =\>  
“sender\_user\_id = #{self.id}”  
has\_many :received\_comments, :class\_name =\> ‘Comment’, :conditions =\>  
“recipient\_user\_id = #{self.id}”  
end

class Comment \< ActiveRecord::Base  
belongs\_to :sender, :class\_name =\> ‘User’, :foreign\_key =\>  
‘sender\_user\_id’  
belongs\_to :recipient, :class =\> ‘User’, :foreign\_key =\>  
‘recipient\_user\_id’  
end

* * *

c = Comment.find\_by\_id N  
…  
puts c.sender.name  
…  
s = c.sender  
c\_of\_s = s.sent\_comments  
…  
c\_of\_s.each do |comment|  
…  
end

---

<div class="post-metadata">

**Author:** ![jjones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jjones](https://www.ruby-forum.com/u/jjones)\
**Post date:** [November 21, 2005, 12:01pm UTC](https://www.ruby-forum.com/t/multiple-user-id-columns/48489/3 "2005-11-21T12:01:47Z")

</div>

class Comment \< ActiveRecord::Base

belongs\_to :author, :class\_name =\> ‘User’, :foreign\_key =\> ‘author\_id’  
belongs\_to :recipient, :class\_name =\> ‘User’, :foreign\_key =\>  
‘recipient\_id’

end

… by making the class and the FK explicit, you can name things  
whatever you like…

- james
