1

I have a student that can have many comments left about them:

class Student < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :student
end

The comment, however, needs to belong to the student to whom it is about but also belong to the student that made the comment. That is, the comment needs to belong to two different students at the same time.

How can this be achieved?

Harry
  • 1,659
  • 5
  • 19
  • 34

1 Answers1

4

In the comments table, you should have a commenter_id and a student_id so a comment can belong to a commenter and also a student.

class Comment < ActiveRecord::Base
  belongs_to :student
  belongs_to :commenter, class_name: 'Student'
end
jvnill
  • 29,479
  • 4
  • 83
  • 86
  • 1
    probably want to add indices on those columns too, but depends on the use case. –  Mar 31 '13 at 16:21