Multiple user_id columns

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

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

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