在做一个学生作业系统的时候出现的问题,Student 通过 Enrollment 与 Course 多对多关联,先上代码
class Course < ApplicationRecord
has_many :enrollments
has_many :students, :through => :enrollments, :source => :user, :conditions => { :type => 'Student' }
end
class Student < User
has_many :enrollments
has_many :courses, :through => :enrollments, :source => :course
end
class Enrollment < ApplicationRecord
belongs_to :course
belongs_to :student
end
在 rails console 里执行 student.courses 的时候系统提示
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :course in model Enrollment. Try 'has_many :courses, :through => :enrollments, :source => <name>'. Is it one of ?
Student 这个 model 是从 User 单表继承过来的,source 那里应该填 user 还是 student?两种都试了下但是一样会出错,能麻烦各位帮忙看一下吗?