Ruby on Rails Double Association
我有一个student
可以留下很多关于他们的comment
:
class Student < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :student end
然而,评论需要属于它所关注的学生,但也属于发表评论的学生。 也就是说,评论需要同时属于两个不同的学生。
怎么能实现这一目标?
在评论表中,您应该有一个commenter_id
和一个student_id
因此评论可以属于评论者和学生。
class Comment < ActiveRecord::Base belongs_to :student belongs_to :commenter, class_name: 'Student' end