Ruby on Rails的双重协会 [英] Ruby on Rails Double Association

查看:180
本文介绍了Ruby on Rails的双重协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个学生,可以有很多的注释的左关于他们:

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?

推荐答案

在意见表中,你应该有一个 commenter_id student_id数据这样的注释可以属于一个评论者,也是一个学生。

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

这篇关于Ruby on Rails的双重协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆