这是this.的后续问题
这是我目前建立师生关系的背景.
用户模型
has_many :teacher_links, :foreign_key => :student_id, :dependent => :destroy, :class_name => "TeacherStudentLink"
has_many :student_links, :foreign_key => :teacher_id, :dependent => :destroy, :class_name => "TeacherStudentLink"
has_many :students, :through => :student_links
has_many :teachers, :through => :teacher_linksTeacherStudentLink模型
class TeacherStudentLink < ActiveRecord::Base
attr_accessible :user_id, :student_id, :teacher_id
belongs_to :user
belongs_to :student, :class_name => "User"
belongs_to :teacher, :class_name => "User"
end我觉得很尴尬,因为teacher_student_links表有三列:用户、学生、教师。用户可以有很多老师,他也可以有很多学生。如果我没有教师专栏,而只是假装“用户”是“老师”,一切都很完美。有办法解决这个问题吗?
发布于 2013-01-22 03:04:30
奶酪黄鼠狼在评论中说,你的链接不应该有user_id
class TeacherStudentLink < ActiveRecord::Base
attr_accessible :student_id, :teacher_id
belongs_to :student, :class_name => "User", :foreign_key => :student_id
belongs_to :teacher, :class_name => "User", :foreign_key => :teacher_id
endhttps://stackoverflow.com/questions/14450537
复制相似问题